Skip to content

Commit cd3f260

Browse files
committed
Create configuration directory if not exists.
Add command "config list" and "config show"
1 parent a416fc4 commit cd3f260

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

godmine/main.go

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -685,16 +685,55 @@ func editWikiPage(title string) error {
685685
return nil
686686
}
687687

688-
func initConfigFile() error {
688+
func initConfigFile(endpoint string, apikey string, project string) error {
689689
template := `{
690-
"endpoint": "http://redmine.example.com",
691-
"apikey": "YOUR-API-KEY",
692-
"project": 1 // default project id
690+
"endpoint": "%s",
691+
"apikey": "%s",
692+
"project": %s
693693
}`
694+
content := fmt.Sprintf(template, endpoint, apikey, project);
694695

695696
file := createConfigFileName()
697+
dir := filepath.Dir(file)
696698

697-
return ioutil.WriteFile(file, []byte(template), 0600)
699+
if _, err := os.Stat(dir); err != nil {
700+
os.MkdirAll(dir, 0755)
701+
}
702+
703+
return ioutil.WriteFile(file, []byte(content), 0600)
704+
}
705+
706+
func listConfigFile() error {
707+
file := createConfigFileName()
708+
dir := filepath.Dir(file)
709+
710+
files, err := ioutil.ReadDir(dir)
711+
if err != nil {
712+
fmt.Println("Directory not exists: %s", dir)
713+
return err
714+
}
715+
716+
for _, file := range files {
717+
fmt.Println(file.Name())
718+
}
719+
720+
return nil
721+
}
722+
723+
func showConfigFile() error {
724+
file := createConfigFileName()
725+
726+
fmt.Println(file)
727+
728+
content, err := ioutil.ReadFile(file)
729+
if err != nil {
730+
fmt.Println("File not exists: %s", file)
731+
return err
732+
}
733+
734+
fmt.Println(string(content))
735+
736+
return nil
698737
}
699738

700739
func editConfigFile() error {
@@ -828,7 +867,13 @@ func main() {
828867
editConfigFile()
829868
break
830869
case "i", "init":
831-
initConfigFile()
870+
initConfigFile(flag.Arg(2), flag.Arg(3), flag.Arg(4))
871+
break
872+
case "l", "list":
873+
listConfigFile()
874+
break
875+
case "s", "show":
876+
showConfigFile()
832877
break
833878
default:
834879
usage()

0 commit comments

Comments
 (0)