-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
115 lines (99 loc) · 2.84 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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* This file is part of AMS SDK
* Copyright 2021 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the Lesser GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, SATISFACTORY
* QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
* License for more details.
*
* You should have received a copy of the Lesser GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"crypto/sha256"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/anbox-cloud/ams-sdk/examples/ams/common"
"github.com/anbox-cloud/ams-sdk/internal/ams/client"
)
type appExportCmd struct {
common.ConnectionCmd
id string
version string
target string
}
func (command *appExportCmd) Parse() {
flag.StringVar(&command.id, "id", "", "Application id")
flag.StringVar(&command.version, "version", "", "Application version to export")
flag.StringVar(&command.target, "target", "", "Output name of exported package")
command.ConnectionCmd.Parse()
if len(command.id) == 0 || len(command.version) == 0 {
flag.Usage()
os.Exit(1)
}
}
func main() {
cmd := &appExportCmd{}
cmd.Parse()
c := cmd.NewClient()
err := exportApplicationVersion(c, cmd.id, cmd.version, cmd.target)
if err != nil {
log.Fatal(err)
}
}
func exportApplicationVersion(c client.Client, id, version, target string) error {
versionNum, err := strconv.Atoi(version)
if err != nil {
log.Fatal(err)
}
file, err := ioutil.TempFile("", "ams_application_export")
if err != nil {
return err
}
defer os.Remove(file.Name())
if len(target) == 0 {
const formatPattern = "2006-01-02-150405"
target = fmt.Sprintf("%s_%s", id, time.Now().Format(formatPattern))
}
return c.ExportApplicationByVersion(id, versionNum, func(header *http.Header, body io.ReadCloser) error {
if _, err = io.Copy(file, body); err != nil {
return err
}
file.Seek(0, 0)
hasher := sha256.New()
_, err = io.Copy(hasher, file)
if err != nil {
return err
}
imageFingerprint := fmt.Sprintf("%x", hasher.Sum(nil))
fingerprint := header.Get("X-AMS-Fingerprint")
if imageFingerprint != fingerprint {
return fmt.Errorf("Fingerprint doesn't match")
}
metaName := header.Get("Content-Disposition")
extension := strings.SplitN(metaName, ".", 2)[1]
if !strings.HasSuffix(target, extension) {
target = fmt.Sprintf("%s.%s", target, extension)
}
if err := os.Rename(file.Name(), target); err != nil {
return err
}
print("Application exported successfully!\n")
return nil
})
}