Skip to content

Commit

Permalink
Update the fly machines autostop flag
Browse files Browse the repository at this point in the history
This flag now accepts the new string values for autostop ("off", "stop",
"suspend") in addition to the original boolean ones.
  • Loading branch information
matttpt committed Jul 16, 2024
1 parent 4b1118b commit bb37439
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
23 changes: 18 additions & 5 deletions internal/command/machine/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math/rand"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -112,10 +113,10 @@ var sharedFlags = flag.Set{
Description: "Automatically start a stopped Machine when a network request is received",
Default: true,
},
flag.Bool{
flag.String{
Name: "autostop",
Description: "Automatically stop a Machine when there are no network requests for it",
Default: true,
NoOptDefVal: "true",
},
flag.String{
Name: "restart",
Expand Down Expand Up @@ -774,10 +775,22 @@ func determineMachineConfig(
}

if flag.IsSpecified(ctx, "autostop") {
if flag.GetBool(ctx, "autostop") {
s.Autostop = fly.Pointer(fly.MachineAutostopStop)
// We'll try to parse it as a boolean first for backward
// compatibility. (strconv.ParseBool is what the pflag
// library uses for booleans under the hood.)
asString := flag.GetString(ctx, "autostop")
if asBool, err := strconv.ParseBool(asString); err == nil {
if asBool {
s.Autostop = fly.Pointer(fly.MachineAutostopStop)
} else {
s.Autostop = fly.Pointer(fly.MachineAutostopOff)
}
} else {
s.Autostop = fly.Pointer(fly.MachineAutostopOff)
var value fly.MachineAutostop
if err := value.UnmarshalText([]byte(asString)); err != nil {
return nil, err
}
s.Autostop = fly.Pointer(value)
}
}

Expand Down
4 changes: 4 additions & 0 deletions internal/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type String struct {
Shorthand string
Description string
Default string
NoOptDefVal string
ConfName string
EnvName string
Hidden bool
Expand All @@ -110,6 +111,9 @@ func (s String) addTo(cmd *cobra.Command) {

f := flags.Lookup(s.Name)
f.Hidden = s.Hidden
if s.NoOptDefVal != "" {
f.NoOptDefVal = s.NoOptDefVal
}

// Aliases
for _, name := range s.Aliases {
Expand Down

0 comments on commit bb37439

Please sign in to comment.