Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package cmd

import (
"encoding/json"
"fmt"
"os"
"strings"
Expand All @@ -12,6 +13,31 @@ import (
"github.com/spf13/pflag"
)

func subscriptionIDFromAzureAuth(subscriptionID, authContents string) string {
if len(subscriptionID) > 0 {
return subscriptionID
}

if len(authContents) == 0 {
return ""
}

authMap := map[string]string{}
if err := json.Unmarshal([]byte(authContents), &authMap); err != nil {
return ""
}

return authMap["subscriptionId"]
}

func sampleDeleteCommand(provider, hostID, subscriptionID string) string {
cmd := fmt.Sprintf("inletsctl delete --provider %s --id \"%s\"", provider, hostID)
if provider == "azure" && len(subscriptionID) > 0 {
cmd += fmt.Sprintf(" --subscription-id %q", subscriptionID)
}
return cmd
}

func getFileOrString(flags *pflag.FlagSet, file, value string, required bool) (string, error) {
var val string
fileVal, _ := flags.GetString(file)
Expand Down
43 changes: 43 additions & 0 deletions cmd/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Inlets Author(s) 2023. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package cmd

import "testing"

func Test_subscriptionIDFromAzureAuth(t *testing.T) {
t.Parallel()

auth := `{"subscriptionId":"sub-from-file","clientId":"id","clientSecret":"secret","tenantId":"tenant"}`

if got := subscriptionIDFromAzureAuth("", auth); got != "sub-from-file" {
t.Fatalf("want sub-from-file, got %q", got)
}

if got := subscriptionIDFromAzureAuth("sub-from-flag", auth); got != "sub-from-flag" {
t.Fatalf("want sub-from-flag, got %q", got)
}

if got := subscriptionIDFromAzureAuth("", "not-json"); got != "" {
t.Fatalf("want empty string, got %q", got)
}
}

func Test_sampleDeleteCommand(t *testing.T) {
t.Parallel()

want := `inletsctl delete --provider digitalocean --id "123"`
if got := sampleDeleteCommand("digitalocean", "123", ""); got != want {
t.Fatalf("want %q, got %q", want, got)
}

want = `inletsctl delete --provider azure --id "group|deployment" --subscription-id "00000000-0000-0000-0000-000000000000"`
if got := sampleDeleteCommand("azure", "group|deployment", "00000000-0000-0000-0000-000000000000"); got != want {
t.Fatalf("want %q, got %q", want, got)
}

want = `inletsctl delete --provider azure --id "group|deployment"`
if got := sampleDeleteCommand("azure", "group|deployment", ""); got != want {
t.Fatalf("want %q, got %q", want, got)
}
}
10 changes: 4 additions & 6 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ inlets-pro http client --url "wss://%s:%d" \
--upstream http://127.0.0.1:8080

To delete:
inletsctl delete --provider %s --id "%s"
%s
`,
inletsProVersion,
hostStatus.IP,
Expand All @@ -380,8 +380,7 @@ To delete:
hostStatus.IP,
inletsProControlPort,
inletsToken,
provider,
hostStatus.ID)
sampleDeleteCommand(provider, hostStatus.ID, subscriptionIDFromAzureAuth(subscriptionID, accessToken)))

return nil
} else {
Expand All @@ -397,16 +396,15 @@ inlets-pro tcp client --url "wss://%s:%d" \
--ports 2222

To delete:
inletsctl delete --provider %s --id "%s"
%s
`,
inletsProVersion,
hostStatus.IP,
inletsToken,
hostStatus.IP,
inletsProControlPort,
inletsToken,
provider,
hostStatus.ID)
sampleDeleteCommand(provider, hostStatus.ID, subscriptionIDFromAzureAuth(subscriptionID, accessToken)))

return nil
}
Expand Down