-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathos.go
107 lines (85 loc) · 2.33 KB
/
os.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
package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"github.com/KarpelesLab/rest"
)
type ShellOs struct {
Id string `json:"Shell_OS__"`
Name string
URL string
Default string // Y|N
Ready string // Y|N
Visible string // Y|N
Beta string // Y|N
Public string // Y|N
Family string // linux|windows|macos|android|unknown
Boot string // guest-linux|bios|efi
CPU string // x86_64
Purpose string // unknown|desktop|server|mobile
Cmdline string // cmdline for guest-linux
Flags map[string]any // byol_warning
}
type ShellOsImage struct {
Id string `json:"Shell_OS_Image__"`
Version string
QA string `json:"QA_Passed"` // Y, P or N
Filename string
Format string
Source string
Status string
Size string // as string because might be too large to fit
Hash string
// Created timestamp
}
func osList(ri *runInfo) error {
// list available shells
var list []ShellOs
// TODO read paging information and fetch all pages if more than one
err := ri.auth.Apply(context.Background(), "Shell/OS", "GET", map[string]any{"results_per_page": 100}, &list)
if err != nil {
return err
}
for _, shos := range list {
fmt.Fprintf(os.Stdout, "%s %s\r\n", shos.Id, shos.Name)
}
return nil
}
func osImgList(ri *runInfo) error {
// list available shells
var list []ShellOsImage
osId := ri.flags["os"]
err := ri.auth.Apply(context.Background(), "Shell/OS/"+osId+"/Image", "GET", map[string]any{}, &list)
if err != nil {
return err
}
for _, img := range list {
fmt.Fprintf(os.Stdout, "%s %s %s\r\n", img.Id, img.Version, img.Filename)
}
return nil
}
func osImgUpload(ri *runInfo) error {
osId := ri.flags["os"]
fn := ri.flags["file"]
fp, err := os.Open(fn)
if err != nil {
return fmt.Errorf("while trying to open file to upload: %w", err)
}
defer fp.Close()
log.Printf("Uploading %s ...", fn)
res, err := rest.Upload(ri.auth.token.Use(context.Background()), "Shell/OS/"+osId+"/Image:upload", "POST", map[string]any{"filename": filepath.Base(fn)}, fp, "application/octet-stream")
if err != nil {
return err
}
var img *ShellOsImage
err = res.Apply(&img)
if err != nil {
return err
}
// show info
fmt.Fprintf(os.Stdout, "%s %s %s\r\n", img.Id, img.Version, img.Filename)
return nil
}