Skip to content

Commit

Permalink
Work on funneling errors to cobra commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandeep Jadoonanan committed Oct 7, 2017
1 parent 0fc396e commit d139e48
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 14 deletions.
5 changes: 4 additions & 1 deletion cmd/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ var ArgsCmd = &cobra.Command{
Long: "Print the SSH command for a host",
Run: func(cmd *cobra.Command, args []string) {
// Resolve filename from flags.
filename := resolveFilename(cmd)
filename, err := resolveFilename(cmd)
if err != nil {
log.Fatal(err)
}

// Check that only one host was specified.
if len(args) != 1 {
Expand Down
5 changes: 4 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func init() {
func InteractivePrompt(cmd *cobra.Command) {
fmt.Println("Starting interactive prompt...")
// Resolve filename from flags.
filename := resolveFilename(cmd)
filename, err := resolveFilename(cmd)
if err != nil {
log.Fatal(err)
}

// Init engine.
konnect, err := engine.Init(filename)
Expand Down
5 changes: 4 additions & 1 deletion cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ var ConnectCmd = &cobra.Command{
Long: "Connect to a host",
Run: func(cmd *cobra.Command, args []string) {
// Resolve filename from flags.
filename := resolveFilename(cmd)
filename, err := resolveFilename(cmd)
if err != nil {
log.Fatal(err)
}

// Check that only one host was specified.
if len(args) != 1 {
Expand Down
6 changes: 4 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var InitCmd = &cobra.Command{
log.Fatal("Too many args. Please specify a directory")
}

// If specified, then use the given directory.
// If a diectory is specified, then use the given directory.
if len(args) == 1 {
dir = args[0]
}
Expand All @@ -42,6 +42,8 @@ var InitCmd = &cobra.Command{
log.Fatalf("File %v already exists.\n", filename)
}

makeDefaultConfig(filename)
if err := makeDefaultConfig(filename); err != nil {
log.Fatal(err)
}
},
}
5 changes: 4 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ var ListCmd = &cobra.Command{
Long: "List all hosts",
Run: func(cmd *cobra.Command, args []string) {
// Resolve filename from flags.
filename := resolveFilename(cmd)
filename, err := resolveFilename(cmd)
if err != nil {
log.Fatal(err)
}

// Check that only one host was specified.
if len(args) != 0 {
Expand Down
5 changes: 4 additions & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ var StatusCmd = &cobra.Command{
Long: "Check the status of one or more hosts",
Run: func(cmd *cobra.Command, args []string) {
// Resolve filename from flags.
filename := resolveFilename(cmd)
filename, err := resolveFilename(cmd)
if err != nil {
log.Fatal(err)
}

// Init engine.
konnect, err := engine.Init(filename)
Expand Down
15 changes: 8 additions & 7 deletions cmd/utils_.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"

yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -36,7 +35,7 @@ func removeDuplicates(elements []string) []string {
// Resolve the config filename from cmd flags.
// Fallback to default filename.
// Validate that the file exists.
func resolveFilename(cmd *cobra.Command) string {
func resolveFilename(cmd *cobra.Command) (string, error) {
// Get config filename from flags.
filename, _ := cmd.Flags().GetString("filename")
wasProvided := true
Expand All @@ -59,13 +58,13 @@ func resolveFilename(cmd *cobra.Command) string {
// Could not find the config file that the user specified.
err = fmt.Errorf("Config %v does not exist", filename)
}
log.Fatal(err)
return "", err
}

return filename
return filename, nil
}

func makeDefaultConfig(filename string) {
func makeDefaultConfig(filename string) error {
// Make default proxylist and konnect engine.
proxyList := map[string]*proxy.SSHProxy{
"app": &proxy.SSHProxy{
Expand All @@ -87,7 +86,7 @@ func makeDefaultConfig(filename string) {
// Marshal konnect struct to a byte slice.
byteSlice, err := yaml.Marshal(konnect)
if err != nil {
log.Fatal(err)
return err
}

// Make config header.
Expand All @@ -104,10 +103,12 @@ func makeDefaultConfig(filename string) {

// Write byte slice to file.
if err = ioutil.WriteFile(filename, data, 0644); err != nil {
log.Fatal(err)
return err
}

fmt.Println("Created configuration file at:")
c := color.New(color.FgCyan, color.Bold)
c.Printf("%v\n", filename)

return nil
}

0 comments on commit d139e48

Please sign in to comment.