-
Notifications
You must be signed in to change notification settings - Fork 234
/
info.go
94 lines (78 loc) · 2.02 KB
/
info.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
package info
import (
"context"
"fmt"
"math"
"time"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/spf13/cobra"
)
func New() *cobra.Command {
const (
long = `Shows information about the application.`
short = `Shows information about the application`
)
cmd := command.New("info", short, long, runInfo,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.NoArgs
cmd.Hidden = true
cmd.Deprecated = "Replaced by 'status', 'ips list', and 'services list'"
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)
return cmd
}
func runInfo(ctx context.Context) error {
var (
client = client.FromContext(ctx).API()
appName = appconfig.NameFromContext(ctx)
)
appInfo, err := client.GetAppInfo(ctx, appName)
if err != nil {
return err
}
if appInfo.PlatformVersion == "machines" {
return showMachineInfo(ctx, appName)
} else {
return showNomadInfo(ctx, appInfo)
}
}
// TODO - Move this into a higher level package, so it can be used elsewhere.
func formatRelativeTime(t time.Time) string {
if t.Before(time.Now()) {
dur := time.Since(t)
if dur.Seconds() < 1 {
return "just now"
}
if dur.Seconds() < 60 {
return fmt.Sprintf("%ds ago", int64(dur.Seconds()))
}
if dur.Minutes() < 60 {
return fmt.Sprintf("%dm%ds ago", int64(dur.Minutes()), int64(math.Mod(dur.Seconds(), 60)))
}
if dur.Hours() < 24 {
return fmt.Sprintf("%dh%dm ago", int64(dur.Hours()), int64(math.Mod(dur.Minutes(), 60)))
}
} else {
dur := time.Until(t)
if dur.Seconds() < 60 {
return fmt.Sprintf("%ds", int64(dur.Seconds()))
}
if dur.Minutes() < 60 {
return fmt.Sprintf("%dm%ds", int64(dur.Minutes()), int64(math.Mod(dur.Seconds(), 60)))
}
if dur.Hours() < 24 {
return fmt.Sprintf("%dh%dm", int64(dur.Hours()), int64(math.Mod(dur.Minutes(), 60)))
}
}
return formatTime(t)
}
func formatTime(t time.Time) string {
return t.Format(time.RFC3339)
}