-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change from urfave/cli to spf13/cobra. Fixes #89
- Loading branch information
Showing
7 changed files
with
490 additions
and
266 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,50 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
homedir "github.com/mitchellh/go-homedir" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var cfgFile string | ||
|
||
var RootCmd = &cobra.Command{ | ||
Use: "kala", | ||
Short: "Modern job scheduler", | ||
Long: `See https://github.com/ajvb/kala for documentation.`, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
return viper.BindPFlags(cmd.Flags()) | ||
}, | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initViper) | ||
RootCmd.PersistentFlags().StringVar(&cfgFile, "kala", "", "config file (default is $HOME/kala.yaml)") | ||
} | ||
|
||
func initViper() { | ||
if cfgFile != "" { | ||
viper.SetConfigFile(cfgFile) | ||
} else { | ||
home, err := homedir.Dir() | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
viper.AddConfigPath(home) | ||
viper.SetConfigName("kala") | ||
} | ||
|
||
viper.SetEnvPrefix("kala") | ||
viper.AutomaticEnv() | ||
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) | ||
|
||
if err := viper.ReadInConfig(); err == nil { | ||
fmt.Println("Using config file:", viper.ConfigFileUsed()) | ||
} | ||
} |
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,38 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/ajvb/kala/job" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var runCommandCmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "run a job", | ||
Long: `runs the specific job immediately and returns the result`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) == 0 { | ||
log.Fatal("Must include a command") | ||
} else if len(args) > 1 { | ||
log.Fatal("Must only include a command") | ||
} | ||
|
||
j := &job.Job{ | ||
Command: args[0], | ||
} | ||
|
||
out, err := j.RunCmd() | ||
if err != nil { | ||
log.Fatalf("Command Failed with err: %s", err) | ||
} else { | ||
fmt.Println("Command Succeeded!") | ||
fmt.Println("Output: ", out) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(runCommandCmd) | ||
} |
Oops, something went wrong.