-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |