From 9a468172fac2d4cfdd87f9b160a711e17416c886 Mon Sep 17 00:00:00 2001 From: Leonardo Di Donato Date: Wed, 25 Sep 2024 15:43:51 +0200 Subject: [PATCH] feat(cmd/ci): dump the settings (tokens part only) into the systemd config file Signed-off-by: Leonardo Di Donato --- cmd/ci/ci.go | 87 ++++++++++++++++++++++++++-------------------------- go.mod | 2 +- go.sum | 8 ++--- 3 files changed, 46 insertions(+), 51 deletions(-) diff --git a/cmd/ci/ci.go b/cmd/ci/ci.go index 1f062df..2729793 100644 --- a/cmd/ci/ci.go +++ b/cmd/ci/ci.go @@ -16,14 +16,17 @@ package ci import ( + "bytes" "context" "fmt" "net/http" "os" + "os/exec" + "path/filepath" "runtime" + "strings" "github.com/cli/cli/pkg/iostreams" - "github.com/davecgh/go-spew/spew" "github.com/listendev/lstn/internal/project" "github.com/listendev/lstn/pkg/ci" "github.com/listendev/lstn/pkg/cmd/flags" @@ -148,14 +151,12 @@ This command requires a listen.dev pro account.`, return fmt.Errorf("got empty settings from the Core API") } coreSettings := *coreResponse.JSON200 - spew.Dump(coreSettings.Tokens) // TODO: complete - io.StopProgressIndicator() c.Println(cs.SuccessIcon(), "Fetch settings") // Create configuration for runtime eavesdropping tool io.StartProgressIndicator() - envConfig := fmt.Sprintf("%s\n%s=%s\n%s=%s\n", info.Dump(), "LISTENDEV_TOKEN", ciOpts.Token.JWT, "GITHUB_TOKEN", ciOpts.Token.GitHub) + envConfig := fmt.Sprintf("%s\n%s\n%s=%s\n%s=%s\n", strings.Join(coreSettings.TokensSlice(), "\n"), info.Dump(), "LISTENDEV_TOKEN", ciOpts.Token.JWT, "GITHUB_TOKEN", ciOpts.Token.GitHub) envDirErr := os.MkdirAll("/var/run/argus", 0750) if envDirErr != nil { io.StopProgressIndicator() @@ -172,47 +173,45 @@ This command requires a listen.dev pro account.`, io.StopProgressIndicator() c.Println(cs.SuccessIcon(), "Wrote config", cs.Magenta(envConfigFilename)) - return nil + io.StartProgressIndicator() + var argus *exec.Cmd + if len(ciOpts.Directory) > 0 { + file := filepath.Join(ciOpts.Directory, "argus") + info, err := os.Stat(file) + if os.IsNotExist(err) { + io.StopProgressIndicator() + + return fmt.Errorf("couldn't find the argus binary in %s", ciOpts.Directory) + } + if info.IsDir() { + io.StopProgressIndicator() - // io.StartProgressIndicator() - // var argus *exec.Cmd - // if len(ciOpts.Directory) > 0 { - // file := filepath.Join(ciOpts.Directory, "argus") - // info, err := os.Stat(file) - // if os.IsNotExist(err) { - // io.StopProgressIndicator() - - // return fmt.Errorf("couldn't find the argus binary in %s", ciOpts.Directory) - // } - // if info.IsDir() { - // io.StopProgressIndicator() - - // return fmt.Errorf("expecting %s to be an executable file", file) - // } - // argus = exec.CommandContext(ctx, file, "-s", "enable-now") - // } else { - // exe, err := exec.LookPath("argus") - // if err != nil { - // io.StopProgressIndicator() - - // return fmt.Errorf("couldn't find the argus executable in the PATH") - // } - // argus = exec.CommandContext(ctx, exe, "-s", "enable-now") - // } - // io.StopProgressIndicator() - // c.Println(cs.Blue("•"), "Install and enable", cs.Magenta(argus.String())) - - // io.StartProgressIndicator() - // argusOut, argusErr := argus.CombinedOutput() - // if argusErr != nil { - // io.StopProgressIndicator() - - // return fmt.Errorf("couldn't install and enable argus: %w", argusErr) - // } - // io.StopProgressIndicator() - // c.Println(string(bytes.Trim(argusOut, "\n"))) - - // return nil + return fmt.Errorf("expecting %s to be an executable file", file) + } + argus = exec.CommandContext(ctx, file, "-s", "enable-now") + } else { + exe, err := exec.LookPath("argus") + if err != nil { + io.StopProgressIndicator() + + return fmt.Errorf("couldn't find the argus executable in the PATH") + } + argus = exec.CommandContext(ctx, exe, "-s", "enable-now") + } + io.StopProgressIndicator() + c.Println(cs.Blue("•"), "Install and enable", cs.Magenta(argus.String())) + + io.StartProgressIndicator() + argusOut, argusErr := argus.CombinedOutput() + if argusErr != nil { + io.StopProgressIndicator() + + return fmt.Errorf("couldn't install and enable argus: %w", argusErr) + } + io.StopProgressIndicator() + c.Println(string(bytes.Trim(argusOut, "\n"))) + + return nil }, } diff --git a/go.mod b/go.mod index 19a78c4..5853069 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/imdario/mergo v0.3.13 github.com/itchyny/gojq v0.12.15 github.com/jarcoal/httpmock v1.3.1 - github.com/listendev/pkg v0.0.0-20240925115341-7cbaafbff31f + github.com/listendev/pkg v0.0.0-20240925130938-87ea6e7dbf41 github.com/matishsiao/goInfo v0.0.0-00010101000000-000000000000 github.com/mitchellh/mapstructure v1.5.0 github.com/muja/goconfig v0.0.0-20180417074348-0a635507dddc diff --git a/go.sum b/go.sum index f1641f4..aadee4b 100644 --- a/go.sum +++ b/go.sum @@ -418,12 +418,8 @@ github.com/leodido/go-npmpackagename v0.2.0 h1:PWtLWmGwffpCMdVY1twLqVw9IyQJVU3Tf github.com/leodido/go-npmpackagename v0.2.0/go.mod h1:dFl2KBJaW8cAAnWZXtrNnXdpDEfg5hmBtg808SrblpM= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= -github.com/listendev/pkg v0.0.0-20240419111038-ecbb9c6e3aa0 h1:5Hjtbsc3FN7Wtz58U41nVG2CidE5XeEOZEEukH/vPm8= -github.com/listendev/pkg v0.0.0-20240419111038-ecbb9c6e3aa0/go.mod h1:5arbKKs4nBpXLTDR7/H7AMCfk57OOnKvMYkBGNWcXCg= -github.com/listendev/pkg v0.0.0-20240924140700-3eb7337428a4 h1:3ssCde8pyywnsc0YqRGe1P9Zfdi4+1nrnUuV+4gpBmk= -github.com/listendev/pkg v0.0.0-20240924140700-3eb7337428a4/go.mod h1:5arbKKs4nBpXLTDR7/H7AMCfk57OOnKvMYkBGNWcXCg= -github.com/listendev/pkg v0.0.0-20240925115341-7cbaafbff31f h1:ZTuQr//S3PELuQtmsx0D1aohMOu6QBzEONZXDvcybvE= -github.com/listendev/pkg v0.0.0-20240925115341-7cbaafbff31f/go.mod h1:5arbKKs4nBpXLTDR7/H7AMCfk57OOnKvMYkBGNWcXCg= +github.com/listendev/pkg v0.0.0-20240925130938-87ea6e7dbf41 h1:og7862I3TboqURhpUbQS+Gv7uDqh86c2hOJXSeLNg1k= +github.com/listendev/pkg v0.0.0-20240925130938-87ea6e7dbf41/go.mod h1:5arbKKs4nBpXLTDR7/H7AMCfk57OOnKvMYkBGNWcXCg= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=