Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adds stricter error checking for file handling sections #3

Open
wants to merge 1 commit into
base: prod
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions genie.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"flag"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -32,7 +33,9 @@ type configFile struct {
func findCommandFiles() []string {
var discoveredFiles []string
homeDirectory, _ := homedir.Dir()
discoveredFiles = append(discoveredFiles, homeDirectory+"/.genie-commands.yaml")
if _, err := os.Stat(homeDirectory + "/.genie-commands.yaml"); !errors.Is(err, os.ErrNotExist) {
discoveredFiles = append(discoveredFiles, homeDirectory+"/.genie-commands.yaml")
}

path, err := os.Getwd()
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
Expand All @@ -52,18 +55,18 @@ func findCommandFiles() []string {
return discoveredFiles
}

func initCommandsFile() string {
func initCommandsFile() (string, error) {
path, _ := os.Getwd()
targetFile := path + "/genie.yaml"

_, err := os.Stat(targetFile)
if err == nil {
log.Fatalf("genie.yaml file already exists at " + path)
return "", errors.New("genie.yaml file already exists at " + path)
}

commandMap := make(map[string][]command)
commandMap["example"] = []command{
command{Command: "echo this is an example command"},
{Command: "echo this is an example command"},
}
t := configFile{
Shell: "/bin/bash",
Expand All @@ -75,13 +78,16 @@ func initCommandsFile() string {
log.Fatalf("Unable to create file " + path)
}

return path
return path, nil
}

func (c *configFile) getConf() *configFile {
files := findCommandFiles()
for k := range files {
yamlFile, err := ioutil.ReadFile(files[k])
for _, path := range files {
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
continue
}
yamlFile, err := ioutil.ReadFile(path)
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
Expand Down Expand Up @@ -113,20 +119,24 @@ func main() {
var c configFile
c.getConf()
args := os.Args[1:]
if 0 == len(args) {
if len(args) == 0 {
c.getAvailableCommands()
os.Exit(0)
}

commandName := flag.Args()[0]

if commandName == "init" {
path := initCommandsFile()
path, err := initCommandsFile()
if err != nil {
log.Println(err)
return
}
fmt.Printf("Created commands file at " + path)
}

for key := range c.Commands[commandName] {
if dryRun == true {
if dryRun {
fmt.Printf(">\t%s\n", c.Commands[commandName][key].Command)
} else {
command := c.Commands[commandName][key].Command
Expand Down