Skip to content

Commit

Permalink
Merge pull request #1862 from lwronski/warn-sip-commnd
Browse files Browse the repository at this point in the history
Warn in sip mode when using restricted command
  • Loading branch information
lwronski authored Feb 16, 2023
2 parents 37f400b + 65b3652 commit c613293
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ScalaCliCommands(
(if (pgpUseBinaryCommands) pgpBinaryCommands.allScalaCommands.toSeq else Nil)

def commands =
allCommands.filter(c => !isSipScala || !c.isRestricted) ++
allCommands ++
(if (pgpUseBinaryCommands) Nil else pgpCommands.allExternalCommands.toSeq) ++
(if (pgpUseBinaryCommands) pgpBinaryCommands.allExternalCommands.toSeq else Nil)

Expand Down
25 changes: 20 additions & 5 deletions modules/cli/src/main/scala/scala/cli/commands/ScalaCommand.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package scala.cli.commands

import caseapp.Name
import caseapp.core.app.Command
import caseapp.core.complete.{Completer, CompletionItem}
import caseapp.core.help.{Help, HelpFormat}
import caseapp.core.parser.Parser
import caseapp.core.util.Formatter
import caseapp.core.{Arg, Error, RemainingArgs}
import caseapp.{HelpMessage, Name}
import coursier.core.{Repository, Version}
import dependency.*

Expand All @@ -19,7 +19,13 @@ import scala.build.internal.{Constants, Runner}
import scala.build.options.{BuildOptions, ScalacOpt, Scope}
import scala.build.{Artifacts, Logger, Positioned, ReplArtifacts}
import scala.cli.commands.default.LegacyScalaOptions
import scala.cli.commands.shared.{HasLoggingOptions, ScalaCliHelp, ScalacOptions, SharedOptions}
import scala.cli.commands.shared.{
HasLoggingOptions,
HelpMessages,
ScalaCliHelp,
ScalacOptions,
SharedOptions
}
import scala.cli.commands.util.CommandHelpers
import scala.cli.commands.util.ScalacOptionsUtil.*
import scala.cli.internal.ProcUtil
Expand All @@ -32,9 +38,10 @@ abstract class ScalaCommand[T <: HasLoggingOptions](implicit myParser: Parser[T]

def sharedOptions(t: T): Option[SharedOptions] = // hello borked unused warning
None
override def hasFullHelp = true

protected var argvOpt = Option.empty[Array[String]]
override def hasFullHelp = true
override def hidden = shouldExcludeInSip
protected var argvOpt = Option.empty[Array[String]]
private val shouldExcludeInSip = isRestricted && !ScalaCli.allowRestrictedFeatures
override def setArgv(argv: Array[String]): Unit = {
argvOpt = Some(argv)
}
Expand Down Expand Up @@ -261,6 +268,11 @@ abstract class ScalaCommand[T <: HasLoggingOptions](implicit myParser: Parser[T]

override def helpFormat: HelpFormat = ScalaCliHelp.helpFormat

override val messages: Help[T] =
if (shouldExcludeInSip)
Help[T](helpMessage = Some(HelpMessage(HelpMessages.restrictedCommandUsedInSip)))
else help

/** @param options
* command-specific [[T]] options
* @return
Expand Down Expand Up @@ -289,6 +301,9 @@ abstract class ScalaCommand[T <: HasLoggingOptions](implicit myParser: Parser[T]
* start of running every [[ScalaCommand]].
*/
final override def run(options: T, remainingArgs: RemainingArgs): Unit = {
if (shouldExcludeInSip)
System.err.println(HelpMessages.restrictedCommandUsedInSip)
sys.exit(1)
CurrentParams.verbosity = options.logging.verbosity
maybePrintWarnings(options)
maybePrintGroupHelp(options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import scala.cli.errors.GitHubApiError

object SecretCreate extends ScalaCommand[SecretCreateOptions] {

override def hidden = false
override def scalaSpecificationLevel = SpecificationLevel.RESTRICTED
override def names = List(
List("github", "secret", "create"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import scala.cli.errors.GitHubApiError

object SecretList extends ScalaCommand[SecretListOptions] {

override def hidden = false
override def scalaSpecificationLevel = SpecificationLevel.RESTRICTED
override def names = List(
List("github", "secret", "list"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ object HelpMessages {
s"""Specific $cmdName configurations can be specified with both command line options and using directives defined in sources.
|Command line options always take priority over using directives when a clash occurs, allowing to override configurations defined in sources.
|Using directives can be defined in all supported input source file types.""".stripMargin
lazy val restrictedCommandUsedInSip: String =
s"""This command is restricted and requires setting the `--power` option to be used.
|You can pass it explicitly or set it globally by running:
| ${Console.BOLD}${ScalaCli.progName} config power true""".stripMargin
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ class SipScalaTests extends ScalaCliSuite {
if (isRestricted) {
expect(res.exitCode == 1)
val output = res.out.text()
expect(
"directories is not a .* sub-command and it is not a valid path to an input file or directory".r
.unanchored
.matches(output)
)
expect(output.contains(
"""This command is restricted and requires setting the `--power` option to be used"""
))
}
else expect(res.exitCode == 0)
}
Expand Down Expand Up @@ -147,11 +145,9 @@ class SipScalaTests extends ScalaCliSuite {
mergeErrIntoOut = true,
env = homeEnv
).out.text().trim
expect(
"package is not a .* sub-command and it is not a valid path to an input file or directory".r
.unanchored
.matches(output)
)
expect(output.contains(
"""This command is restricted and requires setting the `--power` option to be used"""
))
// enable power features
os.proc(TestUtil.cli, "config", "power", "true").call(cwd = root, env = homeEnv).out.trim()
val powerOutput = os.proc(TestUtil.cli, "package").call(
Expand Down

0 comments on commit c613293

Please sign in to comment.