Skip to content

Commit

Permalink
Merge pull request #44 from exitshell/task-36
Browse files Browse the repository at this point in the history
Task-36: Connect up the filesystem
  • Loading branch information
TunedMystic committed Nov 2, 2017
2 parents 5aaabb0 + 9814d6f commit c4d5573
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var InitCmd = &cobra.Command{
dir, _ = filepath.Abs(dir)

// Get the default name for the config file.
localFilename := getDefaultConfig()
localFilename := getDefaultConfigs()[0]

// Join the resolved directory with
// the default config filename.
Expand Down
44 changes: 23 additions & 21 deletions cmd/utils_.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ func handleErr(err error) {
}
}

func getDefaultConfig() string {
return "./konnect.yml"
// This function returns a slice of
// possible default config filenames.
func getDefaultConfigs() []string {
return []string{
"./konnect.yml",
"../konnect.yml",
}
}

// Remove duplicate elements from a string slice.
Expand All @@ -41,36 +46,33 @@ func removeDuplicates(elements []string) []string {
return result
}

// Resolve the config filename from cmd flags.
// Fallback to default filename.
// Get the config filename from cmd flags.
// Fallback to default filenames.
// Validate that the file exists.
func resolveFilename(cmd *cobra.Command) (string, error) {
// Get config filename from flags.
filename, _ := cmd.Flags().GetString("filename")
wasProvided := true
filenames := []string{filename}

// If filename is not specified, then set
// to default config filename.
// If filename is not specified, then get
// a list of possible config filenames.
if filename == "" {
wasProvided = false
filename = getDefaultConfig()
filenames = getDefaultConfigs()
}

// Check if the filename exists.
if _, err := os.Stat(filename); err != nil {
// Could not find a config file.
if wasProvided == false {
err = errors.New("Could not find a " +
"konnect.yml configuration file " +
"in this directory")
} else {
// Could not find the config file that the user specified.
err = fmt.Errorf("Config %v does not exist", filename)
for _, fName := range filenames {
// Check if the filename exists.
if _, err := os.Stat(fName); err == nil {
// Filename was found. Immediately return.
return fName, nil
}
return "", err
}

return filename, nil
// At this point, none of the possible filenames
// were found. Return an error.
err := errors.New("Could not find a " +
"konnect.yml configuration file.")
return "", err
}

func makeDefaultConfig(filename string) error {
Expand Down

0 comments on commit c4d5573

Please sign in to comment.