Skip to content

Commit 4a98d10

Browse files
committed
Better static_datastore cmd test
1 parent 55132bf commit 4a98d10

File tree

5 files changed

+217
-71
lines changed

5 files changed

+217
-71
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"fsys",
2020
"goveralls",
2121
"Hasher",
22+
"homefile",
2223
"jbenet",
2324
"protobuf",
2425
"securefifo",

cmd/static_datastore_builder/main.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright © 2022 Bartłomiej Święcki (byo)
2+
Copyright © 2023 Bartłomiej Święcki (byo)
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -16,8 +16,15 @@ limitations under the License.
1616

1717
package main
1818

19-
import "github.com/cinode/go/pkg/cmd/static_datastore"
19+
import (
20+
"context"
21+
"log"
22+
23+
"github.com/cinode/go/pkg/cmd/static_datastore"
24+
)
2025

2126
func main() {
22-
static_datastore.Execute()
27+
if err := static_datastore.Execute(context.Background()); err != nil {
28+
log.Fatal(err.Error())
29+
}
2330
}

pkg/cmd/static_datastore/compile.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,40 +46,41 @@ func compileCmd() *cobra.Command {
4646
"a content with static files that can then be used to serve through a",
4747
"simple http server.",
4848
}, "\n"),
49-
Run: func(cmd *cobra.Command, args []string) {
49+
RunE: func(cmd *cobra.Command, args []string) error {
5050
if o.srcDir == "" || o.dstLocation == "" {
51-
cmd.Help()
52-
return
51+
return cmd.Help()
5352
}
5453

55-
enc := json.NewEncoder(os.Stdout)
54+
enc := json.NewEncoder(cmd.OutOrStdout())
5655
enc.SetIndent("", " ")
5756

58-
fatalResult := func(format string, args ...interface{}) {
57+
fatalResult := func(format string, args ...interface{}) error {
5958
msg := fmt.Sprintf(format, args...)
6059

6160
enc.Encode(map[string]string{
6261
"result": "ERROR",
6362
"msg": msg,
6463
})
6564

66-
log.Fatalf(msg)
65+
cmd.SilenceUsage = true
66+
cmd.SilenceErrors = true
67+
return errors.New(msg)
6768
}
6869

6970
if len(rootWriterInfoFile) > 0 {
7071
data, err := os.ReadFile(rootWriterInfoFile)
7172
if err != nil {
72-
fatalResult("Couldn't read data from the writer info file at '%s': %v", rootWriterInfoFile, err)
73+
return fatalResult("Couldn't read data from the writer info file at '%s': %v", rootWriterInfoFile, err)
7374
}
7475
if len(data) == 0 {
75-
fatalResult("Writer info file at '%s' is empty", rootWriterInfoFile)
76+
return fatalResult("Writer info file at '%s' is empty", rootWriterInfoFile)
7677
}
7778
rootWriterInfoStr = string(data)
7879
}
7980
if len(rootWriterInfoStr) > 0 {
8081
wi, err := cinodefs.WriterInfoFromString(rootWriterInfoStr)
8182
if err != nil {
82-
fatalResult("Couldn't parse writer info: %v", err)
83+
return fatalResult("Couldn't parse writer info: %v", err)
8384
}
8485
o.writerInfo = wi
8586
}
@@ -91,7 +92,7 @@ func compileCmd() *cobra.Command {
9192

9293
ep, wi, err := compileFS(cmd.Context(), o)
9394
if err != nil {
94-
fatalResult("%s", err)
95+
return fatalResult("%s", err)
9596
}
9697

9798
result := map[string]string{
@@ -104,6 +105,7 @@ func compileCmd() *cobra.Command {
104105
enc.Encode(result)
105106

106107
log.Println("DONE")
108+
return nil
107109
},
108110
}
109111

pkg/cmd/static_datastore/root.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright © 2022 Bartłomiej Święcki (byo)
2+
Copyright © 2023 Bartłomiej Święcki (byo)
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -17,8 +17,7 @@ limitations under the License.
1717
package static_datastore
1818

1919
import (
20-
"fmt"
21-
"os"
20+
"context"
2221

2322
"github.com/spf13/cobra"
2423
)
@@ -50,9 +49,6 @@ node is stored in a plaintext in a file called 'entrypoint.txt'.
5049

5150
// Execute adds all child commands to the root command and sets flags appropriately.
5251
// This is called by main.main(). It only needs to happen once to the rootCmd.
53-
func Execute() {
54-
if err := rootCmd().Execute(); err != nil {
55-
fmt.Println(err)
56-
os.Exit(1)
57-
}
52+
func Execute(ctx context.Context) error {
53+
return rootCmd().ExecuteContext(ctx)
5854
}

0 commit comments

Comments
 (0)