-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
124 lines (100 loc) · 3.3 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
package main
// Copyright 2015 MediaMath <http://www.mediamath.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import (
"flag"
"fmt"
"log"
"os"
"strings"
"time"
)
//examples:
// - ARTIFACTORY_HOST=https://example.com ARTIFACTORY_USER=foo ARTIFACTORY_PASSWORD=bar part -g com.example -a goo -v 1.2 -r local-release goo-jdk1.2.zip
// - part -c example.com.json -g com.example -a goo -v 1.2-SNAPSHOT -r local-snapshot goo.zip
const hostEnvVariable = "ARTIFACTORY_HOST"
var (
verbose = flag.Bool("verbose", false, "Show verbose output.")
getFlag = flag.Bool("get", false, "Get the artifact instead of publishing it.")
pomOnly = flag.Bool("pomOnly", false, "Do NOT publish. Generate poms only")
timeout = flag.String("t", "30s", "Client timeout")
credentialsFile = flag.String("credentials", "", fmt.Sprintf("File with user, password. If .json extension assumes json otherwise ini. If not provided assumes %s, %s environment variables are provided.", userEnvVariable, passwordEnvVariable))
host = flag.String("h", os.Getenv(hostEnvVariable), fmt.Sprintf("Artifactory REST API endpoint (ie https://artifactory.example.com/artifactory/). If not provided looks at environment variable %s.", hostEnvVariable))
repo = flag.String("r", "", "Repository to publish to")
group = flag.String("g", "", "Maven group")
artifact = flag.String("a", "", "Maven artifact")
version = flag.String("v", "", "Maven version")
)
func parseLocations() ([]*location, error) {
creds, err := getCredentials(*credentialsFile)
if err != nil {
return nil, err
}
if len(flag.Args()) < 1 {
return nil, fmt.Errorf("Must provide something to publish")
}
if *host == "" ||
*repo == "" ||
*group == "" ||
*version == "" {
return nil, fmt.Errorf("Must provide all required fields")
}
artifacts := make(map[string][]string)
if *artifact != "" {
artifacts[*artifact] = flag.Args()
} else {
for _, colonDelimited := range flag.Args() {
pair := strings.Split(colonDelimited, ":")
if len(pair) != 2 {
return nil, fmt.Errorf("Could not parse: %v", colonDelimited)
}
artifacts[pair[0]] = append(artifacts[pair[0]], pair[1])
}
}
locations := []*location{}
for artifact, files := range artifacts {
for _, file := range files {
loc := &location{}
loc.creds = creds
loc.host = *host
loc.repo = *repo
loc.group = *group
loc.version = *version
loc.artifact = artifact
loc.file = file
locations = append(locations, loc)
}
}
return locations, nil
}
func main() {
flag.Parse()
locations, err := parseLocations()
if err != nil {
flag.PrintDefaults()
log.Fatal(err)
}
timeoutDuration, parseErr := time.ParseDuration(*timeout)
if parseErr != nil {
log.Printf("Cannout parse timeout, using 30s: %v", parseErr)
timeoutDuration = 30 * time.Second
}
if *getFlag {
for _, loc := range locations {
getErr := getArtifact(loc)
if getErr != nil {
log.Fatal(getErr)
}
}
} else {
for _, loc := range locations {
fileResponse, pomResponse, publishErr := publish(timeoutDuration, *pomOnly, loc)
if publishErr != nil {
log.Fatal(publishErr)
}
fmt.Println(fileResponse.AsString(*verbose))
fmt.Println(pomResponse.AsString(*verbose))
}
}
}