Skip to content

Commit 0e5d2d1

Browse files
KTanAug21Kathryn Anne S Tan
andauthored
Allow fly logs to read --machine flag (#3820)
* Allow fly logs to read --machine flag, throw error if --instance clashes with --machine(they should not clash as they identify the same value, that is a machine id) * test * 1. Include --instance as an alias for --machine for fly logs 2. Remove integration test for fly_logs 3. Create preflight test for fly_logs * Fix trailing spaces, go linter error * Update test name to properly identify what is being tested * Update preflight test for fly logs to get machine id directly from MachineList, remove logic for parsing machine id from fly machines list outpu * Add logic for UseAliasShortHand in makeAlias function. This boolean parameter would allow the inclusion of aliases default shorthands(first character of alias). * Remove the function GetMachineId, as creating an alias for instance already handles its logic, remove test for GetMachineId, add test for shorthand flags for instance(-i) and machine(-m) for fly logs * fix trailing whitespaces, use gofmt to relevant files to pass gh workflow tests * Update the names of subtests in fly_logs_test to better reflect their objectives * Remove shorthand m for machine flag for fly logs * Remove testcase for shorthand -m for fly logs --------- Co-authored-by: Kathryn Anne S Tan <[email protected]>
1 parent 4b3bdbd commit 0e5d2d1

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

internal/command/logs/logs.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ Use --no-tail to only fetch the logs in the buffer.
5050
flag.Region(),
5151
flag.JSONOutput(),
5252
flag.String{
53-
Name: "instance",
54-
Shorthand: "i",
55-
Description: "Filter by instance ID",
53+
Name: "machine",
54+
Description: "Filter by machine ID",
55+
Aliases: []string{"instance"},
56+
UseAliasShortHand: true,
5657
},
5758
flag.Bool{
5859
Name: "no-tail",
@@ -69,7 +70,7 @@ func run(ctx context.Context) error {
6970
opts := &logs.LogOptions{
7071
AppName: appconfig.NameFromContext(ctx),
7172
RegionCode: config.FromContext(ctx).Region,
72-
VMID: flag.GetString(ctx, "instance"),
73+
VMID: flag.GetString(ctx, "machine"),
7374
NoTail: flag.GetBool(ctx, "no-tail"),
7475
}
7576

internal/flag/flag.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
type extraArgsContextKey struct{}
1515

1616
func makeAlias[T any](template T, name string) T {
17+
1718
var ret T
1819
value := reflect.ValueOf(&ret).Elem()
1920

@@ -31,6 +32,15 @@ func makeAlias[T any](template T, name string) T {
3132
if hiddenField.IsValid() {
3233
hiddenField.SetBool(true)
3334
}
35+
36+
useAliasShortHandField := reflect.ValueOf(template).FieldByName("UseAliasShortHand")
37+
if useAliasShortHandField.IsValid() {
38+
useAliasShortHand := useAliasShortHandField.Interface().(bool)
39+
if useAliasShortHand == true {
40+
value.FieldByName("Shorthand").SetString(string(name[0]))
41+
}
42+
}
43+
3444
return ret
3545
}
3646

@@ -88,16 +98,17 @@ func (b Bool) addTo(cmd *cobra.Command) {
8898

8999
// String wraps the set of string flags.
90100
type String struct {
91-
Name string
92-
Shorthand string
93-
Description string
94-
Default string
95-
NoOptDefVal string
96-
ConfName string
97-
EnvName string
98-
Hidden bool
99-
Aliases []string
100-
CompletionFn func(ctx context.Context, cmd *cobra.Command, args []string, partial string) ([]string, error)
101+
Name string
102+
Shorthand string
103+
Description string
104+
Default string
105+
NoOptDefVal string
106+
ConfName string
107+
EnvName string
108+
Hidden bool
109+
Aliases []string
110+
UseAliasShortHand bool
111+
CompletionFn func(ctx context.Context, cmd *cobra.Command, args []string, partial string) ([]string, error)
101112
}
102113

103114
func (s String) addTo(cmd *cobra.Command) {

test/preflight/fly_logs_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//go:build integration
2+
// +build integration
3+
4+
package preflight
5+
6+
import (
7+
"github.com/stretchr/testify/require"
8+
"github.com/superfly/flyctl/test/preflight/testlib"
9+
"testing"
10+
)
11+
12+
13+
func TestFlyLogsMachineFlagBehavior(t *testing.T) {
14+
// Test `flyctl logs` with different flag combinations
15+
16+
// Get library from preflight test lib using env variables form .direnv/preflight
17+
f := testlib.NewTestEnvFromEnv(t)
18+
if f.VMSize != "" {
19+
t.Skip()
20+
}
21+
22+
// Create app, Create Machine, get Machine ID
23+
appName := f.CreateRandomAppMachines()
24+
f.Fly("machine run -a %s nginx --port 80:81 --autostop --region %s", appName, f.PrimaryRegion())
25+
ml := f.MachinesList(appName)
26+
require.Equal(f, 1, len(ml))
27+
machineId := ml[0].ID
28+
29+
// Test if --machine works, should not throw an error
30+
t.Run("TestRunsWhenMachineFlagProvided", func(tt *testing.T) {
31+
f.Fly("logs --app "+appName+" --no-tail --machine " + machineId)
32+
})
33+
34+
// Test if --instance works, should not throw an error
35+
t.Run("TestRunsWhenInstanceFlagProvided", func(tt *testing.T) {
36+
f.Fly("logs --app "+appName+" --no-tail --instance " + machineId)
37+
})
38+
39+
// Test if alias shorthand -i works, should not throw an error
40+
t.Run("TestRunsWhenInstanceShorthandProvided", func(tt *testing.T) {
41+
f.Fly("logs --app "+appName+" --no-tail -i " + machineId)
42+
})
43+
}

0 commit comments

Comments
 (0)