Skip to content

Commit

Permalink
Merge branch 'main' into docker
Browse files Browse the repository at this point in the history
Signed-off-by: David Allen <[email protected]>
  • Loading branch information
davidallendj authored Sep 26, 2024
2 parents e14a856 + 8fba234 commit 8099ca9
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 129 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ lib/%.so: pkg/generator/plugins/%/*.go
mkdir -p lib
go build -buildmode=plugin -o $@ $<

docs:
go doc github.com/OpenCHAMI/cmd
go doc github.com/OpenCHAMI/pkg/configurator

# remove executable and all built plugins
.PHONY: clean
clean:
Expand Down
3 changes: 3 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var configCmd = &cobra.Command{
Short: "Create a new default config file",
Run: func(cmd *cobra.Command, args []string) {
// create a new config at all args (paths)
//
// TODO: change this to only take a single arg since more
// than one arg is *maybe* a mistake
for _, path := range args {
// check and make sure something doesn't exist first
if exists, err := util.PathExists(path); exists || err != nil {
Expand Down
23 changes: 19 additions & 4 deletions cmd/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ package cmd
import (
"fmt"
"net/http"
"os"

"github.com/OpenCHAMI/configurator/pkg/util"
"github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -25,10 +26,24 @@ var fetchCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// make sure a host is set
if remoteHost == "" {
logrus.Errorf("no '--host' argument set")
log.Error().Msg("no '--host' argument set")
return
}

// check to see if an access token is available from env
if config.AccessToken == "" {
// check if OCHAMI_ACCESS_TOKEN env var is set if no access token is provided and use that instead
accessToken := os.Getenv("ACCESS_TOKEN")
if accessToken != "" {
config.AccessToken = accessToken
} else {
// TODO: try and fetch token first if it is needed
if verbose {
fmt.Printf("No token found. Attempting to generate config without one...\n")
}
}
}

// add the "Authorization" header if an access token is supplied
headers := map[string]string{}
if accessToken != "" {
Expand All @@ -40,13 +55,13 @@ var fetchCmd = &cobra.Command{
url := fmt.Sprintf("%s:%d/generate?target=%s", remoteHost, remotePort, target)
res, body, err := util.MakeRequest(url, http.MethodGet, nil, headers)
if err != nil {
logrus.Errorf("failed to make request: %v", err)
log.Error().Err(err).Msg("failed to make request")
return
}
// handle getting other error codes other than a 200
if res != nil {
if res.StatusCode == http.StatusOK {
fmt.Printf("%s\n", string(body))
log.Info().Msgf("%s\n", string(body))
}
}
}
Expand Down
69 changes: 34 additions & 35 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,63 @@ package cmd

import (
"fmt"
"maps"
"io/fs"
"path/filepath"
"strings"

"github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/rodaine/table"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

var (
byTarget bool
pluginDirs []string
generators map[string]generator.Generator
byTarget bool
)

var inspectCmd = &cobra.Command{
Use: "inspect",
Short: "Inspect generator plugin information",
Long: "The 'inspect' sub-command takes a list of directories and prints all found plugin information.",
Run: func(cmd *cobra.Command, args []string) {
// load specific plugins from positional args
generators = make(map[string]generator.Generator)
for _, path := range args {
gen, err := generator.LoadPlugin(path)
if err != nil {
fmt.Printf("failed to load plugin at path '%s': %v\n", path, err)
continue
}
// path is directory, so no plugin is loaded, but no error was returned
if gen == nil {
continue
}
generators[path] = gen
// set up table formatter
table.DefaultHeaderFormatter = func(format string, vals ...interface{}) string {
return strings.ToUpper(fmt.Sprintf(format, vals...))
}

// load plugins and print all plugin details
if len(pluginDirs) > 0 {
// TODO: remove duplicate args from CLI

} else {
for _, pluginDir := range config.PluginDirs {
gens, err := generator.LoadPlugins(pluginDir)
// load specific plugins from positional args
var generators = make(map[string]generator.Generator)
for _, path := range args {
err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
if err != nil {
fmt.Printf("failed to load plugin: %v\n", err)
continue
return err
}
maps.Copy(generators, gens)
if info.IsDir() {
return nil
}
gen, err := generator.LoadPlugin(path)
if err != nil {
return err
}
generators[gen.GetName()] = gen
return nil
})

if err != nil {
log.Error().Err(err).Msg("failed to walk directory")
continue
}
}

// print all generator information
// print all generator plugin information found
tbl := table.New("Name", "Version", "Description")
for _, g := range generators {
tbl.AddRow(g.GetName(), g.GetVersion(), g.GetDescription())
}
if len(generators) > 0 {
o := ""
for _, g := range generators {
o += fmt.Sprintf("- Name: %s\n", g.GetName())
o += fmt.Sprintf(" Version: %s\n", g.GetVersion())
o += fmt.Sprintf(" Description: %s\n", g.GetDescription())
o += "\n"
}
o = strings.TrimRight(o, "\n")
fmt.Printf("%s", o)
tbl.Print()
}
},
}
Expand Down
24 changes: 14 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,39 @@ go 1.21.5

require (
github.com/OpenCHAMI/jwtauth/v5 v5.0.0-20240321222802-e6cb468a2a18
github.com/go-chi/chi/v5 v5.0.12
github.com/lestrrat-go/jwx v1.2.29
github.com/go-chi/chi/v5 v5.1.0
github.com/lestrrat-go/jwx/v2 v2.1.1
github.com/nikolalohinski/gonja/v2 v2.2.0
github.com/openchami/chi-middleware/auth v0.0.0-20240812224658-b16b83c70700
github.com/openchami/chi-middleware/log v0.0.0-20240812224658-b16b83c70700
github.com/rodaine/table v1.2.0
github.com/rs/zerolog v1.33.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/lestrrat-go/backoff/v2 v2.0.8 // indirect
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
github.com/lestrrat-go/httprc v1.0.4 // indirect
github.com/lestrrat-go/httprc v1.0.6 // indirect
github.com/lestrrat-go/iter v1.0.2 // indirect
github.com/lestrrat-go/jwx/v2 v2.0.20 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
)
Loading

0 comments on commit 8099ca9

Please sign in to comment.