/
utils.go
140 lines (120 loc) · 3.64 KB
/
utils.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package bacalhau
import (
"bytes"
"context"
"fmt"
"os"
"testing"
"time"
"github.com/Masterminds/semver"
"github.com/filecoin-project/bacalhau/pkg/executor"
"github.com/filecoin-project/bacalhau/pkg/ipfs"
"github.com/filecoin-project/bacalhau/pkg/publicapi"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
const (
JSONFormat string = "json"
YAMLFormat string = "yaml"
)
var listOutputFormat string
var tableOutputWide bool
var tableHideHeader bool
var tableMaxJobs int
var tableSortBy ColumnEnum
var tableSortReverse bool
var tableIDFilter string
var tableNoStyle bool
func shortenTime(t time.Time) string { //nolint:unused // Useful function, holding here
if tableOutputWide {
return t.Format("06-01-02-15:04:05")
}
return t.Format("15:04:05")
}
var DefaultShortenStringLength = 20
func shortenString(st string) string {
if tableOutputWide {
return st
}
if len(st) < DefaultShortenStringLength {
return st
}
return st[:20] + "..."
}
func shortID(id string) string {
if tableOutputWide {
return id
}
return id[:8]
}
func getAPIClient() *publicapi.APIClient {
return publicapi.NewAPIClient(fmt.Sprintf("http://%s:%d", apiHost, apiPort))
}
// ensureValidVersion checks that the server version is the same or less than the client version
func ensureValidVersion(ctx context.Context, clientVersion *executor.VersionInfo, serverVersion *executor.VersionInfo) error {
if clientVersion == nil {
log.Warn().Msg("Unable to parse nil client version, skipping version check")
return nil
}
if clientVersion.GitVersion == "v0.0.0-xxxxxxx" {
log.Info().Msg("Development version, skipping version check")
return nil
}
if serverVersion == nil {
log.Warn().Msg("Unable to parse nil server version, skipping version check")
return nil
}
c, err := semver.NewVersion(clientVersion.GitVersion)
if err != nil {
log.Warn().Err(err).Msg("Unable to parse client version, skipping version check")
return nil
}
s, err := semver.NewVersion(serverVersion.GitVersion)
if err != nil {
log.Warn().Err(err).Msg("Unable to parse server version, skipping version check")
return nil
}
if s.GreaterThan(c) {
return fmt.Errorf("server version %s is newer than client version %s", serverVersion.GitVersion, clientVersion.GitVersion)
}
return nil
}
func ExecuteTestCobraCommand(t *testing.T, root *cobra.Command, args ...string) (
c *cobra.Command, output string, err error) { //nolint:unparam // use of t is valuable here
buf := new(bytes.Buffer)
root.SetOut(buf)
root.SetErr(buf)
root.SetArgs([]string{})
root.SetArgs(args)
// Need to check if we're running in debug mode for VSCode
// Empty them if they exist
if (len(os.Args) > 2) && (os.Args[1] == "-test.run") {
os.Args[1] = ""
os.Args[2] = ""
}
log.Trace().Msgf("Command to execute: %v", root.CalledAs())
c, err = root.ExecuteC()
return c, buf.String(), err
}
// TODO: #233 Replace when we move to go1.18
// https://stackoverflow.com/questions/27516387/what-is-the-correct-way-to-find-the-min-between-two-integers-in-go
func Min(a, b int) int {
if a < b {
return a
}
return b
}
func ReverseList(s []string) []string {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
func setupDownloadFlags(cmd *cobra.Command, settings *ipfs.DownloadSettings) {
cmd.Flags().IntVar(&settings.TimeoutSecs, "download-timeout-secs",
settings.TimeoutSecs, "Timeout duration for IPFS downloads.")
cmd.Flags().StringVar(&settings.OutputDir, "output-dir",
settings.OutputDir, "Directory to write the output to.")
cmd.Flags().StringVar(&settings.IPFSSwarmAddrs, "ipfs-swarm-addrs",
settings.IPFSSwarmAddrs, "Comma-separated list of IPFS nodes to connect to.")
}