/
export.go
111 lines (96 loc) · 2.89 KB
/
export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package cmd
import (
"fmt"
"os"
"github.com/nao1215/gorky/file"
"github.com/nao1215/gup/internal/config"
"github.com/nao1215/gup/internal/goutil"
"github.com/nao1215/gup/internal/print"
"github.com/spf13/cobra"
)
func newExportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export",
Short: "Export the binary names under $GOPATH/bin and their path info. to gup.conf.",
Long: `Export the binary names under $GOPATH/bin and their path info. to gup.conf.
Use the export subcommand if you want to install the same golang
binaries across multiple systems. By default, this sub-command
exports the file to $XDG_CONFIG_HOME/.config/gup/gup.conf (e.g. $HOME/.config/gup/gup.conf.)
After you have placed gup.conf in the same path hierarchy on
another system, you execute import subcommand. gup start the
installation according to the contents of gup.conf.`,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
Run: func(cmd *cobra.Command, args []string) {
OsExit(export(cmd, args))
},
}
cmd.Flags().BoolP("output", "o", false, "print command path information at STDOUT")
return cmd
}
func export(cmd *cobra.Command, _ []string) int {
if err := goutil.CanUseGoCmd(); err != nil {
print.Err(fmt.Errorf("%s: %w", "you didn't install golang", err))
return 1
}
output, err := cmd.Flags().GetBool("output")
if err != nil {
print.Err(fmt.Errorf("%s: %w", "can not parse command line argument (--output)", err))
return 1
}
pkgs, err := getPackageInfo()
if err != nil {
print.Err(err)
return 1
}
pkgs = validPkgInfo(pkgs)
if len(pkgs) == 0 {
print.Err("no package information")
return 1
}
if output {
err = outputConfig(pkgs)
} else {
err = writeConfigFile(pkgs)
}
if err != nil {
print.Err(err)
return 1
}
return 0
}
func writeConfigFile(pkgs []goutil.Package) error {
if err := os.MkdirAll(config.DirPath(), file.FileModeCreatingDir); 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 func() {
if closeErr := file.Close(); closeErr != nil {
// TODO: If use go 1.20, rewrite like this.
// err = errors.Join(err, closeErr)
err = closeErr // overwrite error
}
}()
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 {
if v.ImportPath == "" {
print.Warn("can't get '" + v.Name + "'package path information. old go version binary")
continue
}
result = append(result, goutil.Package{Name: v.Name, ImportPath: v.ImportPath, Version: v.Version})
}
return result
}