Skip to content

Commit

Permalink
Move eject file writing into separate func.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed May 5, 2020
1 parent 6d8feb5 commit cb3d29f
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions cmd/eject.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,8 @@ automatically).`,
fmt.Println("All flag used, eject all core files.")
for _, file := range allEjectableFiles {
filePath := "layout/ejected" + file
os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
writeCoreFileErr := ioutil.WriteFile(filePath, generated.Ejected[file], os.ModePerm)
if writeCoreFileErr != nil {
fmt.Printf("Unable to write file: %v\n", writeCoreFileErr)
}
content := generated.Ejected[file]
ejectFile(filePath, content)
}
return
}
Expand All @@ -69,12 +66,8 @@ automatically).`,
}
if confirmed == "Yes" {
filePath := "layout/ejected" + result
fmt.Printf("Ejecting: %s\n", filePath)
os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
writeCoreFileErr := ioutil.WriteFile(filePath, generated.Ejected[result], os.ModePerm)
if writeCoreFileErr != nil {
fmt.Printf("Unable to write file: %v\n", writeCoreFileErr)
}
content := generated.Ejected[result]
ejectFile(filePath, content)
}
if confirmed == "No" {
fmt.Println("No file was ejected.")
Expand All @@ -92,13 +85,8 @@ automatically).`,
}
if fileExists {
filePath := "layout/ejected" + arg
os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
writeCoreFileErr := ioutil.WriteFile(filePath, generated.Ejected[arg], os.ModePerm)
if writeCoreFileErr != nil {
fmt.Printf("Unable to write file: %v\n", writeCoreFileErr)
} else {
fmt.Printf("Ejected %s\n", filePath)
}
content := generated.Ejected[arg]
ejectFile(filePath, content)
} else {
fmt.Printf("There is no ejectable file named %s. Run 'plenti eject' to see list of ejectable files.\n", arg)
}
Expand All @@ -121,3 +109,13 @@ func init() {
// ejectCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
ejectCmd.Flags().BoolVarP(&EjectAll, "all", "a", false, "Eject all core files")
}

func ejectFile(filePath string, content []byte) {
os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
writeCoreFileErr := ioutil.WriteFile(filePath, content, os.ModePerm)
if writeCoreFileErr != nil {
fmt.Printf("Unable to write file: %v\n", writeCoreFileErr)
} else {
fmt.Printf("Ejected %s\n", filePath)
}
}

0 comments on commit cb3d29f

Please sign in to comment.