Skip to content

Commit

Permalink
Merge pull request #4194 from snyk/chore/HMMR-574_default_local_exten…
Browse files Browse the repository at this point in the history
…sions

Refactor to support local extensions
  • Loading branch information
PeterSchafer authored Dec 7, 2022
2 parents 7a6c44c + 125a9b3 commit 1a67e66
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 68 deletions.
80 changes: 60 additions & 20 deletions cliv2/cmd/cliv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"io/ioutil"
"log"
"os"
"strings"

"github.com/snyk/cli/cliv2/internal/cliv2"
"github.com/snyk/cli/cliv2/internal/constants"
"github.com/snyk/cli/cliv2/pkg/basic_workflows"
"github.com/snyk/go-application-framework/pkg/analytics"
"github.com/snyk/go-application-framework/pkg/app"
"github.com/snyk/go-application-framework/pkg/configuration"
localworkflows "github.com/snyk/go-application-framework/pkg/local_workflows"
"github.com/snyk/go-application-framework/pkg/workflow"
"github.com/snyk/go-httpauth/pkg/httpauth"
"github.com/spf13/cobra"
Expand All @@ -19,28 +22,63 @@ import (
var engine workflow.Engine
var config configuration.Configuration
var helpProvided bool
var debugLogger = log.New(os.Stderr, "", 0)

func getDebugLogger(config configuration.Configuration) *log.Logger {
debugLogger := log.New(os.Stderr, "", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
debug := config.GetBool(configuration.DEBUG)

if !debug {
debugLogger.SetOutput(ioutil.Discard)
} else {
debugFlags := config.GetInt(configuration.DEBUG_FORMAT)
debugLogger.SetFlags(debugFlags)
debugLogger.SetPrefix("main - ")
}

return debugLogger
}

func main() {
args := basic_workflows.FilteredArgs()
errorCode := MainWithErrorCode(args)
errorCode := MainWithErrorCode()
os.Exit(errorCode)
}

func getFullCommandString(cmd *cobra.Command) string {
fullCommandPath := []string{cmd.Name()}
fn := func(c *cobra.Command) {
if c.HasParent() {
fullCommandPath = append(fullCommandPath, c.Name())
}
}
cmd.VisitParents(fn)

for i, j := 0, len(fullCommandPath)-1; i < j; i, j = i+1, j-1 {
fullCommandPath[i], fullCommandPath[j] = fullCommandPath[j], fullCommandPath[i]
}

name := strings.Join(fullCommandPath, " ")
return name
}

// main workflow
func runCommand(cmd *cobra.Command, args []string) error {
//fmt.Println("runCommand()", cmd)
return nil

config.AddFlagSet(cmd.Flags())

name := getFullCommandString(cmd)
debugLogger.Println("Running", name)

if len(args) > 0 {
config.Set("targetDirectory", args[0])
}

data, err := engine.Invoke(workflow.NewWorkflowIdentifier(name))
if err == nil {
_, err = engine.InvokeWithInput(workflow.NewWorkflowIdentifier("output"), data)
} else {
debugLogger.Println("Failed to execute the command!", err)
}

return err
}

func sendAnalytics(analytics analytics.Analytics, debugLogger *log.Logger) {
Expand All @@ -54,11 +92,6 @@ func sendAnalytics(analytics analytics.Analytics, debugLogger *log.Logger) {
}
}

func usage(cmd *cobra.Command) error {
// just ensure to do nothing when incorrectly used from cobra side
return nil
}

func help(cmd *cobra.Command, args []string) {
helpProvided = true
defaultCmd(cmd, args)
Expand Down Expand Up @@ -115,42 +148,49 @@ func prepareRootCommand() *cobra.Command {
// some static/global cobra configuration
rootCommand.CompletionOptions.DisableDefaultCmd = true
rootCommand.SilenceErrors = true
rootCommand.SilenceUsage = true
rootCommand.FParseErrWhitelist.UnknownFlags = true

// ensure that help and usage information comes from the legacy cli instead of cobra's default help
rootCommand.SetHelpFunc(help)
rootCommand.SetUsageFunc(usage)
rootCommand.SetHelpCommand(&helpCommand)
rootCommand.PersistentFlags().AddFlagSet(getGlobalFLags())

return &rootCommand
}

func MainWithErrorCode(args []string) int {
func MainWithErrorCode() int {
var err error

rootCommand := prepareRootCommand()
rootCommand.ParseFlags(os.Args)

config = configuration.New()
config.AddFlagSet(rootCommand.Flags())
// create engine
engine = app.CreateAppEngine()
config = engine.GetConfiguration()
config.AddFlagSet(rootCommand.LocalFlags())

debugLogger := getDebugLogger(config)

if noProxyAuth := config.GetBool(basic_workflows.PROXY_NOAUTH); noProxyAuth {
config.Set(configuration.PROXY_AUTHENTICATION_MECHANISM, httpauth.StringFromAuthenticationMechanism(httpauth.NoAuth))
}

// create engine
engine = workflow.NewWorkFlowEngine(config)

// initialize the extensions -> they register themselves at the engine
basic_workflows.Init(engine)
engine.AddExtensionInitializer(basic_workflows.Init)

// init engine
err = engine.Init()
if err != nil {
debugLogger.Println("Failed to init Workflow Engine!", err)
return constants.SNYK_EXIT_CODE_ERROR
}

// add output flags as persistent flags
outputWorkflow, _ := engine.GetWorkflow(localworkflows.WORKFLOWID_OUTPUT_WORKFLOW)
outputFlags := workflow.FlagsetFromConfigurationOptions(outputWorkflow.GetConfigurationOptions())
rootCommand.PersistentFlags().AddFlagSet(outputFlags)

// add workflows as commands
createCommandsForWorkflows(rootCommand, engine)

Expand All @@ -164,7 +204,7 @@ func MainWithErrorCode(args []string) int {
// init Analytics
cliAnalytics := engine.GetAnalytics()
cliAnalytics.SetVersion(cliv2.GetFullVersion())
cliAnalytics.SetCmdArguments(args)
cliAnalytics.SetCmdArguments(os.Args[1:])
if config.GetBool(configuration.ANALYTICS_DISABLED) == false {
defer sendAnalytics(cliAnalytics, debugLogger)
}
Expand Down
3 changes: 1 addition & 2 deletions cliv2/cmd/cliv2/main_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main_test

import (
"os"
"testing"

main "github.com/snyk/cli/cliv2/cmd/cliv2"
Expand All @@ -10,6 +9,6 @@ import (
)

func Test_MainWithErrorCode(t *testing.T) {
err := main.MainWithErrorCode(os.Args[1:])
err := main.MainWithErrorCode()
assert.Equal(t, err, 0)
}
6 changes: 3 additions & 3 deletions cliv2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ require (
github.com/elazarl/goproxy v0.0.0-20220901064549-fbd10ff4f5a1
github.com/elazarl/goproxy/ext v0.0.0-20220901064549-fbd10ff4f5a1
github.com/google/uuid v1.3.0
github.com/snyk/go-application-framework v0.0.0-20221130192253-dca471134d75
github.com/pkg/errors v0.9.1
github.com/snyk/go-application-framework v0.0.0-20221201105605-4873440314c9
github.com/snyk/go-httpauth v0.0.0-20220915135832-0edf62cf8cdd
github.com/spf13/cobra v1.6.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.0
)

Expand All @@ -30,12 +32,10 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.13.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b // indirect
Expand Down
26 changes: 2 additions & 24 deletions cliv2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,30 +182,8 @@ github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYe
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/snyk/go-application-framework v0.0.0-20221021170832-a2eb401fb4fd h1:p2XXK7ew8LFqJZLRHdfu9TOiQ1OTR4YenTNeqdkcaBY=
github.com/snyk/go-application-framework v0.0.0-20221021170832-a2eb401fb4fd/go.mod h1:VPLG3e3ObtzHMEtF6kI8gjKvpWhl05AII73JFeSxD6Y=
github.com/snyk/go-application-framework v0.0.0-20221023120849-8b5c05264aeb h1:xEtov7y1nG845xLk9mLm1P/KjEf9aZhIM/iiHewf1mI=
github.com/snyk/go-application-framework v0.0.0-20221023120849-8b5c05264aeb/go.mod h1:VPLG3e3ObtzHMEtF6kI8gjKvpWhl05AII73JFeSxD6Y=
github.com/snyk/go-application-framework v0.0.0-20221024162253-bc93902a2b03 h1:2SdAlqLfquSN9jHByc6j+30JGI9i9pzbH9AYhukwwjM=
github.com/snyk/go-application-framework v0.0.0-20221024162253-bc93902a2b03/go.mod h1:Ux4mZwrdevzQk3RWqJagzMqL+VuuTApXitcfwejepu8=
github.com/snyk/go-application-framework v0.0.0-20221025171438-454bf9a4a44c h1:9MB500LIbSEhLRxfeabR/YPz+sQGlLVo1RtaawdXDaM=
github.com/snyk/go-application-framework v0.0.0-20221025171438-454bf9a4a44c/go.mod h1:Ux4mZwrdevzQk3RWqJagzMqL+VuuTApXitcfwejepu8=
github.com/snyk/go-application-framework v0.0.0-20221025172551-70ebdcd986ce h1:zieJdkt6bldoddcr1SLbiU4UcQ2js5sbJ77cT97LElY=
github.com/snyk/go-application-framework v0.0.0-20221025172551-70ebdcd986ce/go.mod h1:Ux4mZwrdevzQk3RWqJagzMqL+VuuTApXitcfwejepu8=
github.com/snyk/go-application-framework v0.0.0-20221025173602-c263720cdc35 h1:zmsDyuEuEuAmQfAP+zOmF83+FalYPfdMp5GLRkaI7Bo=
github.com/snyk/go-application-framework v0.0.0-20221025173602-c263720cdc35/go.mod h1:qxy9kRGPDMbgKqTFHMYTGBaSovjxgIxbSSNtz/fz79I=
github.com/snyk/go-application-framework v0.0.0-20221025173839-fc68f0744320 h1:9hxhtDjgpRMLeIM2fe/Ona9eF3lnwKM8DeitrINja5o=
github.com/snyk/go-application-framework v0.0.0-20221025173839-fc68f0744320/go.mod h1:qxy9kRGPDMbgKqTFHMYTGBaSovjxgIxbSSNtz/fz79I=
github.com/snyk/go-application-framework v0.0.0-20221025200517-6f4e73f57936 h1:0wg5EysSS+WU3UCVn24Ic00alNoPaNHuXYn+0EA72FY=
github.com/snyk/go-application-framework v0.0.0-20221025200517-6f4e73f57936/go.mod h1:qxy9kRGPDMbgKqTFHMYTGBaSovjxgIxbSSNtz/fz79I=
github.com/snyk/go-application-framework v0.0.0-20221027171945-500667ff5926 h1:AWGfpBpI1nJ7cEHxwGzjbVeqUz8u1HLlKRtFqHjqywk=
github.com/snyk/go-application-framework v0.0.0-20221027171945-500667ff5926/go.mod h1:qxy9kRGPDMbgKqTFHMYTGBaSovjxgIxbSSNtz/fz79I=
github.com/snyk/go-application-framework v0.0.0-20221027172303-127a5c059ca3 h1:bkQUoCFtqUc7GmCHtoFcxYx4lX2teJ32jrQ91x+desE=
github.com/snyk/go-application-framework v0.0.0-20221027172303-127a5c059ca3/go.mod h1:qxy9kRGPDMbgKqTFHMYTGBaSovjxgIxbSSNtz/fz79I=
github.com/snyk/go-application-framework v0.0.0-20221028125644-66016a1d4215 h1:0/1AW7/GHT4pAuzIFw0BLOH3jPKf3H4B2eQbTT2jZT0=
github.com/snyk/go-application-framework v0.0.0-20221028125644-66016a1d4215/go.mod h1:qxy9kRGPDMbgKqTFHMYTGBaSovjxgIxbSSNtz/fz79I=
github.com/snyk/go-application-framework v0.0.0-20221130192253-dca471134d75 h1:GJ4pWvuOxI4d1QchO54z/j3sMSCro4tfrOsq3CaTvSE=
github.com/snyk/go-application-framework v0.0.0-20221130192253-dca471134d75/go.mod h1:qxy9kRGPDMbgKqTFHMYTGBaSovjxgIxbSSNtz/fz79I=
github.com/snyk/go-application-framework v0.0.0-20221201105605-4873440314c9 h1:TAIP7A1Yy9qHAW2hoztvNo2z3iodYJT87XKmH/Ty6Hc=
github.com/snyk/go-application-framework v0.0.0-20221201105605-4873440314c9/go.mod h1:cmEA75r0NIy0dyNx5AIKLtczEg6YF0oOPXIKHWiLyWc=
github.com/snyk/go-httpauth v0.0.0-20220915135832-0edf62cf8cdd h1:zjDhcQ642rIVI8aIjfG5uVcw+OGotQtX2l9VHe7IqCQ=
github.com/snyk/go-httpauth v0.0.0-20220915135832-0edf62cf8cdd/go.mod h1:v6t6wKizOcHXT3p4qKn6Bda7yNIjCQ54Xyl31NjgXkY=
github.com/spf13/afero v1.9.2 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw=
Expand Down
20 changes: 17 additions & 3 deletions cliv2/internal/cliv2/cliv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cliv2
import (
_ "embed"
"fmt"
"io"
"log"
"os"
"os/exec"
Expand All @@ -24,6 +25,9 @@ type CLI struct {
DebugLogger *log.Logger
CacheDirectory string
v1BinaryLocation string
stdin io.Reader
stdout io.Writer
stderr io.Writer
}

type EnvironmentWarning struct {
Expand Down Expand Up @@ -51,6 +55,9 @@ func NewCLIv2(cacheDirectory string, debugLogger *log.Logger) (*CLI, error) {
DebugLogger: debugLogger,
CacheDirectory: cacheDirectory,
v1BinaryLocation: v1BinaryLocation,
stdin: os.Stdin,
stdout: os.Stdout,
stderr: os.Stderr,
}

err = cli.ExtractV1Binary()
Expand Down Expand Up @@ -225,9 +232,6 @@ func PrepareV1Command(cmd string, args []string, proxyInfo *proxy.ProxyInfo, int

snykCmd = exec.Command(cmd, args...)
snykCmd.Env, err = PrepareV1EnvironmentVariables(os.Environ(), integrationName, integrationVersion, proxyAddress, proxyInfo.CertificateLocation)
snykCmd.Stdin = os.Stdin
snykCmd.Stdout = os.Stdout
snykCmd.Stderr = os.Stderr

return snykCmd, err
}
Expand All @@ -244,6 +248,10 @@ func (c *CLI) executeV1Default(proxyInfo *proxy.ProxyInfo, passthroughArgs []str
GetFullVersion(),
)

snykCmd.Stdin = c.stdin
snykCmd.Stdout = c.stdout
snykCmd.Stderr = c.stderr

if err != nil {
if evWarning, ok := err.(EnvironmentWarning); ok {
fmt.Println("WARNING! ", evWarning)
Expand Down Expand Up @@ -292,3 +300,9 @@ func DeriveExitCode(err error) int {
func (e EnvironmentWarning) Error() string {
return e.message
}

func (c *CLI) SetIoStreams(stdin io.Reader, stdout io.Writer, stderr io.Writer) {
c.stdin = stdin
c.stdout = stdout
c.stderr = stderr
}
Loading

0 comments on commit 1a67e66

Please sign in to comment.