Skip to content

Commit

Permalink
Add init command for projects file and improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
yigitozgumus committed Jul 7, 2022
1 parent b3a59a6 commit 814b213
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
4 changes: 3 additions & 1 deletion cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func registerAppCommand() {
app.Action = func(context *cli.Context) error {
_, err := lazydraft.GetProjectConfig()
if err != nil {
return err
fmt.Println(err.Error())
return nil
}
if context.NArg() == 0 {
fmt.Println("\n use 'lazydraft help' to see available commands")
Expand All @@ -30,6 +31,7 @@ func registerAppCommand() {

func registerCommands() {
app.Commands = []*cli.Command{
registerInitCommand(),
registerProjectCommand(),
registerDraftCommand(),
}
Expand Down
39 changes: 39 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd

import (
"fmt"
"github.com/urfave/cli/v2"
"io/ioutil"
"lazy-publish/lazydraft"
"os"
)

func registerInitCommand() *cli.Command {
return &cli.Command{
Name: "init",
Aliases: []string{"i"},
Usage: "Create projects.yml to start tracking your projects",
Action: func(context *cli.Context) error {
configFile := lazydraft.ConfigFileName
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
configFilePath := homeDir + "/" + configFile
_, err = os.ReadFile(configFilePath)
if err == nil {
fmt.Println("\nprojects.yml file is present")
return nil
}
fmt.Println("\nCreating projects.yml ...")
configDir := homeDir + "/" + lazydraft.ConfigBaseDir + "/" + lazydraft.ConfigFileDir
_, err = os.ReadDir(configDir)
if err != nil {
os.Mkdir(configDir, 0777)
}
ioutil.WriteFile(configFilePath, []byte{}, 0666)
fmt.Printf("\nprojects.yml is created at '%s'\n", configFilePath)
return nil
},
}
}
11 changes: 7 additions & 4 deletions lazydraft/inputFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"os"
)

const projectFileDoesNotExistsError = "lazydraft project config file does not exist"
const configFileName = ".config/lazydraft/projects.yml"
const projectFileDoesNotExistsError = "\nprojects.yml file could not be found. See 'lazydraft help init'"
const projectFormatInvalidError = "\nprojects.yml file format is invalid. See 'lazydraft config'"
const ConfigFileName = ".config/lazydraft/projects.yml"
const ConfigFileDir = "lazydraft"
const ConfigBaseDir = ".config"
const userHomeDirectoryError = "user home directory cannot be retrieved"
const configFilePathError = "lazydraft file path cannot be retrieved"

Expand All @@ -30,7 +33,7 @@ func (yf InputFile) readProjectConfig() (*ProjectConfig, error) {
}
err = yaml.Unmarshal(data, &projectConfig.Data)
if err != nil {
return nil, errors.New(projectFileDoesNotExistsError)
return nil, errors.New(projectFormatInvalidError)
}
return &projectConfig, nil
}
Expand All @@ -53,7 +56,7 @@ func getConfigFilePath() (*InputFile, error) {
return nil, errors.New(userHomeDirectoryError)
}
configFile := InputFile{
Path: dirname + "/" + configFileName,
Path: dirname + "/" + ConfigFileName,
}
return &configFile, nil
}

0 comments on commit 814b213

Please sign in to comment.