/
version.go
134 lines (110 loc) · 3.23 KB
/
version.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
package app
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/TIBCOSoftware/flogo-cli/cli"
toml "github.com/pelletier/go-toml"
)
var optVersion = &cli.OptionInfo{
Name: "version",
UsageLine: "version",
Short: "displays the version of flogo cli",
Long: `Get the current version number of the cli.
`,
}
type rawLock struct {
SolveMeta solveMeta `toml:"solve-meta"`
Projects []rawLockedProject `toml:"projects"`
}
type solveMeta struct {
InputsDigest string `toml:"inputs-digest"`
AnalyzerName string `toml:"analyzer-name"`
AnalyzerVersion int `toml:"analyzer-version"`
SolverName string `toml:"solver-name"`
SolverVersion int `toml:"solver-version"`
}
type rawLockedProject struct {
Name string `toml:"name"`
Branch string `toml:"branch,omitempty"`
Revision string `toml:"revision"`
Version string `toml:"version,omitempty"`
Source string `toml:"source,omitempty"`
Packages []string `toml:"packages"`
}
const lockName = "Gopkg.lock"
func init() {
CommandRegistry.RegisterCommand(&cmdVersion{option: optVersion})
}
type cmdVersion struct {
option *cli.OptionInfo
}
// HasOptionInfo implementation of cli.HasOptionInfo.OptionInfo
func (c *cmdVersion) OptionInfo() *cli.OptionInfo {
return c.option
}
// Exec implementation of cli.Command.Exec
func (c *cmdVersion) AddFlags(fs *flag.FlagSet) {
//op op
}
// Exec implementation of cli.Command.Exec
func (c *cmdVersion) Exec(args []string) error {
cmd := exec.Command("git", "describe", "--tags")
gopath, set := os.LookupEnv("GOPATH")
if !set {
out, err := exec.Command("go", "env", "GOPATH").Output()
if err != nil {
log.Fatal(err)
}
gopath = strings.TrimSuffix(string(out), "\n")
}
cmd.Dir = filepath.Join(gopath, "src", "github.com", "TIBCOSoftware", "flogo-cli")
cmd.Env = append(os.Environ())
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
re := regexp.MustCompile("\\n")
fc := re.ReplaceAllString(string(out), "")
line := fmt.Sprintf("flogo cli version [%s]\n", fc)
fmt.Fprint(os.Stdout, line)
appDir, err := os.Getwd()
if err != nil {
fmt.Fprint(os.Stderr, "Error: Unable to determine working directory\n\n")
os.Exit(2)
}
project, _ := SetupExistingProjectEnvWithOrWithoutExitOnFailure(appDir, false)
if project != nil {
config, err := toml.LoadFile(filepath.Join(project.GetAppDir(), lockName))
if err != nil {
fmt.Println("Error ", err.Error())
} else {
raw := rawLock{}
err := config.Unmarshal(&raw)
if err != nil {
fmt.Printf("Unable to parse the lock as TOML")
}
for _, v := range raw.Projects {
if caseInsensitiveContains(v.Name, "flogo") {
if v.Version == "" {
line = fmt.Sprintf("Your project uses %s branch %s and revision %s\n", v.Name, v.Branch, v.Revision)
} else {
line = fmt.Sprintf("Your project uses %s version %s\n", v.Name, v.Version)
}
fmt.Fprint(os.Stdout, line)
}
}
}
}
return nil
}
// This isn't the most performant way, but this will be able to check if the string exists while ignoring any case sensitivity.
func caseInsensitiveContains(s, substr string) bool {
s, substr = strings.ToUpper(s), strings.ToUpper(substr)
return strings.Contains(s, substr)
}