Skip to content

Commit

Permalink
Add run stub (#35)
Browse files Browse the repository at this point in the history
* Add run stub

* Hide run stub

* Add UOR_DEV_MODE environment variable to cli

* Get example root command name from os.Args[0]

Co-authored-by: d10n <[email protected]>
  • Loading branch information
d10n and d10n authored Aug 18, 2022
1 parent 6b0d956 commit bf6ea57
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"os"
"path/filepath"
"strconv"

"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
Expand All @@ -12,12 +13,19 @@ import (
"github.com/uor-framework/uor-client-go/cli/log"
)

// EnvConfig stores CLI runtime configuration from environment variables.
// Struct field names should match the name of the environment variable that the field is derived from.
type EnvConfig struct {
UOR_DEV_MODE bool // true: show unimplemented stubs in --help
}

// RootOptions describe global configuration options that can be set.
type RootOptions struct {
IOStreams genericclioptions.IOStreams
LogLevel string
Logger log.Logger
cacheDir string
EnvConfig
}

var clientLong = templates.LongDesc(
Expand Down Expand Up @@ -47,6 +55,7 @@ func NewRootCmd() *cobra.Command {
Out: os.Stdout,
ErrOut: os.Stderr,
}
o.EnvConfig = readEnvConfig()
cmd := &cobra.Command{
Use: filepath.Base(os.Args[0]),
Short: "UOR Client",
Expand Down Expand Up @@ -87,7 +96,18 @@ func NewRootCmd() *cobra.Command {
cmd.AddCommand(NewBuildCmd(&o))
cmd.AddCommand(NewPushCmd(&o))
cmd.AddCommand(NewPullCmd(&o))
cmd.AddCommand(NewRunCmd(&o))
cmd.AddCommand(NewVersionCmd(&o))

return cmd
}

func readEnvConfig() EnvConfig {
envConfig := EnvConfig{}

devModeString := os.Getenv("UOR_DEV_MODE")
devMode, err := strconv.ParseBool(devModeString)
envConfig.UOR_DEV_MODE = err == nil && devMode

return envConfig
}
71 changes: 71 additions & 0 deletions cli/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cli

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/uor-framework/uor-client-go/util/examples"
)

// RunOptions describe configuration options that can
// be set using the run subcommand.
type RunOptions struct {
*RootOptions
Config string
}

var clientRunExamples = examples.Example{
RootCommand: filepath.Base(os.Args[0]),
Descriptions: []string{"Run instructions against a UOR collection."},
CommandString: "run ./config.yaml",
}

// NewRunCmd creates a new cobra.Command for the run subcommand.
func NewRunCmd(rootOpts *RootOptions) *cobra.Command {
o := RunOptions{RootOptions: rootOpts}

cmd := &cobra.Command{
Hidden: !o.UOR_DEV_MODE, // TODO delete after implementation
Use: "run <CONFIG>",
Short: "Run instructions against a UOR collection",
Example: examples.FormatExamples(clientRunExamples),
SilenceErrors: false,
SilenceUsage: false,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(o.Complete(args))
cobra.CheckErr(o.Validate())
cobra.CheckErr(o.Run(cmd.Context()))
},
}

return cmd
}

func (o *RunOptions) Complete(args []string) error {
if len(args) < 1 {
return errors.New("bug: expecting 1 argument")
}
o.Config = args[0]
return nil
}

func (o *RunOptions) Validate() error {
configInfo, err := os.Stat(o.Config)
if err != nil {
return fmt.Errorf("unable to read config file: %v", err)
}
if !configInfo.Mode().IsRegular() {
return errors.New("config file must be a regular file")
}
return nil
}

func (o *RunOptions) Run(ctx context.Context) error {
o.Logger.Infof("stub: command run not yet implemented\n")
return nil
}

0 comments on commit bf6ea57

Please sign in to comment.