This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
106 lines (94 loc) · 2.96 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
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/base64"
"fmt"
"net/http"
"os"
"github.com/appc/acpush/Godeps/_workspace/src/github.com/coreos/rkt/rkt/config"
"github.com/appc/acpush/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/appc/acpush/lib"
)
var (
flagDebug bool
flagInsecure bool
flagUser string
flagPassword string
flagSystemConfigDir string
flagLocalConfigDir string
cmdACPush = &cobra.Command{
Use: "acpush [OPTIONS] IMAGE SIGNATURE URL",
Short: "A utility for pushing ACI files to remote servers",
Run: runACPush,
}
)
func init() {
cmdACPush.Flags().BoolVar(&flagDebug, "debug", false, "Enables debug messages")
cmdACPush.Flags().BoolVar(&flagInsecure, "insecure", false, "Permits unencrypted traffic")
cmdACPush.Flags().StringVar(&flagUser, "username", "", "HTTP Username")
cmdACPush.Flags().StringVar(&flagPassword, "password", "", "HTTP Password")
cmdACPush.Flags().StringVar(&flagSystemConfigDir, "system-conf", "/usr/lib/rkt", "Directory for system configuration")
cmdACPush.Flags().StringVar(&flagLocalConfigDir, "local-conf", "/etc/rkt", "Directory for local configuration")
}
func main() {
cmdACPush.Execute()
}
func runACPush(cmd *cobra.Command, args []string) {
if len(args) != 3 {
cmd.Usage()
os.Exit(1)
}
conf, err := config.GetConfigFrom(flagSystemConfigDir, flagLocalConfigDir)
if err != nil {
fmt.Fprintf(os.Stderr, "error loading config: %v\n", err)
os.Exit(2)
}
err = lib.Uploader{
Acipath: args[0],
Ascpath: args[1],
Uri: args[2],
Insecure: flagInsecure,
Debug: flagDebug,
SetHTTPHeaders: func(r *http.Request) {
if r.URL == nil {
return
}
if flagUser != "" && flagPassword != "" {
creds := []byte(fmt.Sprintf("%s:%s", flagUser, flagPassword))
encodedCreds := base64.StdEncoding.EncodeToString(creds)
r.Header["Authorization"] = append(r.Header["Authorization"], "Basic "+encodedCreds)
} else {
headerer, ok := conf.AuthPerHost[r.URL.Host]
if !ok {
if flagDebug {
fmt.Fprintf(os.Stderr, "No auth present in config for domain %s.\n", r.URL.Host)
}
return
}
header := headerer.Header()
for k, v := range header {
r.Header[k] = append(r.Header[k], v...)
}
}
},
}.Upload()
if err != nil {
fmt.Fprintf(os.Stderr, "err: %v\n", err)
os.Exit(1)
}
if flagDebug {
fmt.Fprintln(os.Stderr, "Upload successful")
}
}