This repository has been archived by the owner on May 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (115 loc) · 2.76 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/adgear/git-tags-resource/actions"
"github.com/adgear/git-tags-resource/services"
"github.com/adgear/git-tags-resource/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var version = "local"
var (
rootCmd = &cobra.Command{}
// Flags
action string
destination string
source string
tmpdir string
verbose bool
showVersion bool
help bool
stdin string
)
func init() {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stderr)
log.SetLevel(log.InfoLevel)
rootCmd.PersistentFlags().StringVarP(&action, "action", "a", "check", "Concourse resource action, defaults to check. Can be check, in or out.")
rootCmd.PersistentFlags().StringVarP(&destination, "destination", "d", "./", "Destination of input resource")
rootCmd.PersistentFlags().StringVarP(&source, "source", "s", "./", "Source of output resource")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose mode.")
rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "V", false, "Prints the version and exit.")
rootCmd.PersistentFlags().BoolVarP(&help, "help", "h", false, "Show help.")
rootCmd.PersistentFlags().StringVarP(&stdin, "stdin", "S", "", "Feed file instead of stdin")
err := rootCmd.Execute()
if err != nil {
fmt.Println(err)
}
}
func main() {
if verbose {
log.SetLevel(log.DebugLevel)
}
if help {
rootCmd.Usage()
os.Exit(0)
}
if showVersion {
log.Info("Version: " + version)
os.Exit(0)
}
var input []byte
var err error
if stdin != "" {
input, err = ioutil.ReadFile(stdin)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
} else {
input, err = ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
}
var inputMap utils.Input
err = json.Unmarshal(input, &inputMap)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
if inputMap.Source.TagFilter == "" {
log.Fatal("missing tag filter...")
os.Exit(1)
}
gts, err := services.NewGitTagsService(inputMap.Source.PrivateKey, inputMap.Source.PrivateKeyPassword)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
switch action {
case "check":
checkResource, err := actions.NewCheckResource(gts)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
output, err := checkResource.Execute(inputMap.Source)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
fmt.Println(output)
os.Exit(0)
case "in":
inResource, err := actions.NewInResource(gts)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
output, err := inResource.Execute(inputMap.Source, destination, inputMap.Version["ref"])
if err != nil {
log.Fatal(err)
os.Exit(1)
}
fmt.Println(output)
os.Exit(0)
default:
fmt.Println("Nope")
os.Exit(1)
}
}