Skip to content

Commit

Permalink
Added output option to Export subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 committed Sep 23, 2022
1 parent 84a0627 commit c18aaf0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
42 changes: 36 additions & 6 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ according to the contents of gup.conf.`,
}

func init() {
exportCmd.Flags().BoolP("output", "o", false, "print command path information at STDOUT")
rootCmd.AddCommand(exportCmd)
}

Expand All @@ -36,8 +37,9 @@ func export(cmd *cobra.Command, args []string) int {
return 1
}

if err := os.MkdirAll(config.DirPath(), 0775); err != nil {
print.Err(fmt.Errorf("%s: %w", "can not make config directory", err))
output, err := cmd.Flags().GetBool("output")
if err != nil {
print.Err(fmt.Errorf("%s: %w", "can not parse command line argument", err))
return 1
}

Expand All @@ -53,14 +55,42 @@ func export(cmd *cobra.Command, args []string) int {
return 1
}

if err := config.WriteConfFile(pkgs); err != nil {
print.Err(err)
return 1
if output {
if err := outputConfig(pkgs); err != nil {
print.Err(err)
return 1
}
} else {
if err := writeConfigFile(pkgs); err != nil {
print.Err(err)
return 1
}
}
print.Info("Export " + config.FilePath())
return 0
}

func writeConfigFile(pkgs []goutil.Package) error {
if err := os.MkdirAll(config.DirPath(), 0775); err != nil {
return fmt.Errorf("%s: %w", "can not make config directory", err)
}

file, err := os.Create(config.FilePath())
if err != nil {
return fmt.Errorf("%s %s: %w", "can't update", config.FilePath(), err)
}
defer file.Close()

if err := config.WriteConfFile(file, pkgs); err != nil {
return err
}
print.Info("Export " + config.FilePath())
return nil
}

func outputConfig(pkgs []goutil.Package) error {
return config.WriteConfFile(os.Stdout, pkgs)
}

func validPkgInfo(pkgs []goutil.Package) []goutil.Package {
result := []goutil.Package{}
for _, v := range pkgs {
Expand Down
10 changes: 2 additions & 8 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,14 @@ func ReadConfFile() ([]goutil.Package, error) {
}

// WriteConfFile write package information at configuration-file.
func WriteConfFile(pkgs []goutil.Package) error {
file, err := os.Create(FilePath())
if err != nil {
return fmt.Errorf("%s %s: %w", "can't update", FilePath(), err)
}
defer file.Close()

func WriteConfFile(file *os.File, pkgs []goutil.Package) error {
text := ""
for _, v := range pkgs {
// lost version information
text = text + fmt.Sprintf("%s = %s\n", v.Name, v.ImportPath)
}

_, err = file.Write(([]byte)(text))
_, err := file.Write(([]byte)(text))
if err != nil {
return fmt.Errorf("%s %s: %w", "can't update", FilePath(), err)
}
Expand Down

0 comments on commit c18aaf0

Please sign in to comment.