Skip to content

Commit ea41886

Browse files
authored
Merge pull request #50 from nao1215/export-pkg-info-at-stdout
Added output option to Export subcommand
2 parents 84a0627 + 61ad148 commit ea41886

File tree

4 files changed

+185
-14
lines changed

4 files changed

+185
-14
lines changed

cmd/export.go

+32-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ according to the contents of gup.conf.`,
2727
}
2828

2929
func init() {
30+
exportCmd.Flags().BoolP("output", "o", false, "print command path information at STDOUT")
3031
rootCmd.AddCommand(exportCmd)
3132
}
3233

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

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

@@ -53,14 +55,40 @@ func export(cmd *cobra.Command, args []string) int {
5355
return 1
5456
}
5557

56-
if err := config.WriteConfFile(pkgs); err != nil {
58+
if output {
59+
err = outputConfig(pkgs)
60+
} else {
61+
err = writeConfigFile(pkgs)
62+
}
63+
if err != nil {
5764
print.Err(err)
5865
return 1
5966
}
60-
print.Info("Export " + config.FilePath())
6167
return 0
6268
}
6369

70+
func writeConfigFile(pkgs []goutil.Package) error {
71+
if err := os.MkdirAll(config.DirPath(), 0775); err != nil {
72+
return fmt.Errorf("%s: %w", "can not make config directory", err)
73+
}
74+
75+
file, err := os.Create(config.FilePath())
76+
if err != nil {
77+
return fmt.Errorf("%s %s: %w", "can't update", config.FilePath(), err)
78+
}
79+
defer file.Close()
80+
81+
if err := config.WriteConfFile(file, pkgs); err != nil {
82+
return err
83+
}
84+
print.Info("Export " + config.FilePath())
85+
return nil
86+
}
87+
88+
func outputConfig(pkgs []goutil.Package) error {
89+
return config.WriteConfFile(os.Stdout, pkgs)
90+
}
91+
6492
func validPkgInfo(pkgs []goutil.Package) []goutil.Package {
6593
result := []goutil.Package{}
6694
for _, v := range pkgs {

cmd/export_test.go

+68-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
"github.com/google/go-cmp/cmp"
14+
"github.com/nao1215/gup/internal/config"
1415
"github.com/nao1215/gup/internal/file"
1516
"github.com/nao1215/gup/internal/goutil"
1617
"github.com/nao1215/gup/internal/print"
@@ -175,7 +176,7 @@ func Test_export(t *testing.T) {
175176
gobin: filepath.Join("testdata", "dummy"),
176177
want: 1,
177178
stderr: []string{
178-
"gup:ERROR: can't get binary-paths installed by 'go install': open " + filepath.Join("testdata", "dummy") + ": no such file or directory",
179+
"gup:ERROR: can't get binary-paths installed by 'go install': open testdata/dummy: no such file or directory",
179180
"",
180181
},
181182
})
@@ -214,6 +215,7 @@ func Test_export(t *testing.T) {
214215
print.Stdout = pw
215216
print.Stderr = pw
216217

218+
tt.args.cmd.Flags().BoolP("output", "o", false, "print command path information at STDOUT")
217219
if got := export(tt.args.cmd, tt.args.args); got != tt.want {
218220
t.Errorf("export() = %v, want %v", got, tt.want)
219221
}
@@ -241,3 +243,68 @@ func Test_export(t *testing.T) {
241243
})
242244
}
243245
}
246+
247+
func Test_export_parse_error(t *testing.T) {
248+
t.Run("parse argument error", func(t *testing.T) {
249+
orgStdout := print.Stdout
250+
orgStderr := print.Stderr
251+
pr, pw, err := os.Pipe()
252+
if err != nil {
253+
t.Fatal(err)
254+
}
255+
print.Stdout = pw
256+
print.Stderr = pw
257+
258+
if got := export(&cobra.Command{}, []string{}); got != 1 {
259+
t.Errorf("export() = %v, want %v", got, 1)
260+
}
261+
pw.Close()
262+
print.Stdout = orgStdout
263+
print.Stderr = orgStderr
264+
265+
buf := bytes.Buffer{}
266+
_, err = io.Copy(&buf, pr)
267+
if err != nil {
268+
t.Error(err)
269+
}
270+
defer pr.Close()
271+
got := strings.Split(buf.String(), "\n")
272+
273+
want := []string{
274+
"gup:ERROR: can not parse command line argument: flag accessed but not defined: output",
275+
"",
276+
}
277+
if diff := cmp.Diff(want, got); diff != "" {
278+
t.Errorf("value is mismatch (-want +got):\n%s", diff)
279+
}
280+
})
281+
}
282+
283+
func Test_writeConfigFile(t *testing.T) {
284+
type args struct {
285+
pkgs []goutil.Package
286+
}
287+
tests := []struct {
288+
name string
289+
args args
290+
wantErr bool
291+
}{
292+
{
293+
name: "failed to open config file",
294+
args: args{
295+
pkgs: []goutil.Package{},
296+
},
297+
wantErr: true,
298+
},
299+
}
300+
for _, tt := range tests {
301+
t.Run(tt.name, func(t *testing.T) {
302+
config.ConfigFileName = ""
303+
defer func() { config.ConfigFileName = "gup.conf" }()
304+
305+
if err := writeConfigFile(tt.args.pkgs); (err != nil) != tt.wantErr {
306+
t.Errorf("writeConfigFile() error = %v, wantErr %v", err, tt.wantErr)
307+
}
308+
})
309+
}
310+
}

cmd/root_test.go

+79
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,85 @@ subaru = github.com/nao1215/subaru
410410
}
411411
}
412412

413+
func TestExecute_Export_WithOutputOption(t *testing.T) {
414+
type test struct {
415+
name string
416+
gobin string
417+
args []string
418+
want []string
419+
}
420+
421+
tests := []test{}
422+
if runtime.GOOS == "windows" {
423+
tests = append(tests, test{
424+
name: "success",
425+
gobin: filepath.Join("testdata", "check_success_for_windows"),
426+
args: []string{"gup", "export", "--output"},
427+
want: []string{
428+
"gal.exe = github.com/nao1215/gal/cmd/gal",
429+
"posixer.exe = github.com/nao1215/posixer",
430+
""},
431+
})
432+
} else {
433+
tests = append(tests, test{
434+
name: "success",
435+
gobin: filepath.Join("testdata", "check_success"),
436+
args: []string{"gup", "export", "--output"},
437+
want: []string{
438+
"gal = github.com/nao1215/gal/cmd/gal",
439+
"posixer = github.com/nao1215/posixer",
440+
"subaru = github.com/nao1215/subaru",
441+
""},
442+
})
443+
}
444+
445+
for _, tt := range tests {
446+
oldGoBin := os.Getenv("GOBIN")
447+
if err := os.Setenv("GOBIN", tt.gobin); err != nil {
448+
t.Fatal(err)
449+
}
450+
defer func() {
451+
if err := os.Setenv("GOBIN", oldGoBin); err != nil {
452+
t.Fatal(err)
453+
}
454+
}()
455+
456+
OsExit = func(code int) {}
457+
defer func() {
458+
OsExit = os.Exit
459+
}()
460+
461+
orgStdout := os.Stdout
462+
orgStderr := os.Stderr
463+
pr, pw, err := os.Pipe()
464+
if err != nil {
465+
t.Fatal(err)
466+
}
467+
os.Stdout = pw
468+
os.Stderr = pw
469+
470+
os.Args = tt.args
471+
t.Run(tt.name, func(t *testing.T) {
472+
Execute()
473+
})
474+
pw.Close()
475+
os.Stdout = orgStdout
476+
os.Stderr = orgStderr
477+
478+
buf := bytes.Buffer{}
479+
_, err = io.Copy(&buf, pr)
480+
if err != nil {
481+
t.Error(err)
482+
}
483+
defer pr.Close()
484+
got := strings.Split(buf.String(), "\n")
485+
486+
if diff := cmp.Diff(tt.want, got); diff != "" {
487+
t.Errorf("value is mismatch (-want +got):\n%s", diff)
488+
}
489+
}
490+
}
491+
413492
func TestExecute_Import(t *testing.T) {
414493
OsExit = func(code int) {}
415494
defer func() {

internal/config/config.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ import (
1313
"github.com/nao1215/gup/internal/goutil"
1414
)
1515

16+
// ConfigFileName is gup command configuration file
17+
var ConfigFileName = "gup.conf"
18+
1619
// FilePath return configuration-file path.
1720
func FilePath() string {
18-
return filepath.Join(DirPath(), "gup.conf")
21+
return filepath.Join(DirPath(), ConfigFileName)
1922
}
2023

2124
// DirPath return directory path that store configuration-file.
@@ -58,20 +61,14 @@ func ReadConfFile() ([]goutil.Package, error) {
5861
}
5962

6063
// WriteConfFile write package information at configuration-file.
61-
func WriteConfFile(pkgs []goutil.Package) error {
62-
file, err := os.Create(FilePath())
63-
if err != nil {
64-
return fmt.Errorf("%s %s: %w", "can't update", FilePath(), err)
65-
}
66-
defer file.Close()
67-
64+
func WriteConfFile(file *os.File, pkgs []goutil.Package) error {
6865
text := ""
6966
for _, v := range pkgs {
7067
// lost version information
7168
text = text + fmt.Sprintf("%s = %s\n", v.Name, v.ImportPath)
7269
}
7370

74-
_, err = file.Write(([]byte)(text))
71+
_, err := file.Write(([]byte)(text))
7572
if err != nil {
7673
return fmt.Errorf("%s %s: %w", "can't update", FilePath(), err)
7774
}

0 commit comments

Comments
 (0)