This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathup.go
89 lines (83 loc) · 2.53 KB
/
up.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
// Copyright 2020 DigitalOcean
// 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 (
c "context"
"flag"
"github.com/digitalocean/godo"
"github.com/google/subcommands"
"time"
)
type upCmd struct {
distro string
region string
slug string
}
func (*upCmd) Name() string { return "up" }
func (*upCmd) Synopsis() string { return "Allows you to start up a new disposable droplet." }
func (*upCmd) Usage() string {
return `up:
Allows you to start up a new disposable droplet.
`
}
func (p *upCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.distro, "distro", "", "Sets the distro slug. Will default to the newest Debian release.")
f.StringVar(&p.region, "region", "", "Sets the region. Will default to the default region within the config.")
f.StringVar(&p.slug, "size", "", "Sets the size slug of the droplet you want. Will default to the default size slug within the config.")
}
func getLatestDebian(distros []godo.Image) string {
debian := make([]godo.Image, 0, 1)
for _, v := range distros {
if v.Distribution == "Debian" {
debian = append(debian, v)
}
}
if len(debian) == 0 {
panic("can't find latest debian x64")
}
var latest godo.Image
var latestTime time.Time
for _, v := range debian {
t, err := time.ParseInLocation("2006-01-02T15:04:05Z", v.Created, time.UTC)
if err != nil {
panic(err)
}
if latestTime.Before(t) {
latest = v
latestTime = t
}
}
return latest.Slug
}
func (p *upCmd) Execute(_ c.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
clientInit()
if p.region == "" {
p.region = config.DefaultRegion
}
if p.slug == "" {
p.slug = config.DefaultSize
}
if p.distro == "" {
distros, resp, err := client.Images.ListDistribution(context(), &godo.ListOptions{})
if err != nil {
if resp != nil && resp.StatusCode == 401 {
println("Please run do-disposable auth to reset your token.")
return subcommands.ExitFailure
}
panic(err)
}
p.distro = getLatestDebian(distros)
}
handleDisposableDroplet(p.region, p.slug, p.distro)
return subcommands.ExitSuccess
}