Skip to content
Merged
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
35 changes: 35 additions & 0 deletions cmd/client_node_user_ssh_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2026 John Dewey

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

package cmd

import (
"github.com/spf13/cobra"
)

// clientNodeUserSSHKeyCmd represents the user ssh-key command.
var clientNodeUserSSHKeyCmd = &cobra.Command{
Use: "ssh-key",
Short: "Manage SSH authorized keys",
}

func init() {
clientNodeUserCmd.AddCommand(clientNodeUserSSHKeyCmd)
}
88 changes: 88 additions & 0 deletions cmd/client_node_user_ssh_key_add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2026 John Dewey

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

package cmd

import (
"fmt"

"github.com/spf13/cobra"

"github.com/retr0h/osapi/internal/cli"
"github.com/retr0h/osapi/pkg/sdk/client"
)

// clientNodeUserSSHKeyAddCmd represents the user ssh-key add command.
var clientNodeUserSSHKeyAddCmd = &cobra.Command{
Use: "add",
Short: "Add an SSH authorized key",
Run: func(cmd *cobra.Command, _ []string) {
ctx := cmd.Context()
host, _ := cmd.Flags().GetString("target")
name, _ := cmd.Flags().GetString("name")
key, _ := cmd.Flags().GetString("key")

resp, err := sdkClient.User.AddKey(ctx, host, name, client.SSHKeyAddOpts{Key: key})
if err != nil {
cli.HandleError(err, logger)
return
}

if jsonOutput {
fmt.Println(string(resp.RawJSON()))
return
}

if resp.Data.JobID != "" {
fmt.Println()
cli.PrintKV("Job ID", resp.Data.JobID)
}

results := make([]cli.MutationResultRow, 0, len(resp.Data.Results))
for _, r := range resp.Data.Results {
var errPtr *string
if r.Error != "" {
errPtr = &r.Error
}
changed := r.Changed
results = append(results, cli.MutationResultRow{
Hostname: r.Hostname,
Status: r.Status,
Changed: &changed,
Error: errPtr,
Fields: []string{fmt.Sprintf("%t", r.Changed)},
})
}
headers, rows := cli.BuildMutationTable(results, []string{"CHANGED"})
cli.PrintCompactTable([]cli.Section{{Headers: headers, Rows: rows}})
},
}

func init() {
clientNodeUserSSHKeyCmd.AddCommand(clientNodeUserSSHKeyAddCmd)

clientNodeUserSSHKeyAddCmd.PersistentFlags().
String("name", "", "Username to add SSH key for (required)")
clientNodeUserSSHKeyAddCmd.PersistentFlags().
String("key", "", "Full SSH public key line (required)")

_ = clientNodeUserSSHKeyAddCmd.MarkPersistentFlagRequired("name")
_ = clientNodeUserSSHKeyAddCmd.MarkPersistentFlagRequired("key")
}
98 changes: 98 additions & 0 deletions cmd/client_node_user_ssh_key_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright (c) 2026 John Dewey

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

package cmd

import (
"fmt"

"github.com/spf13/cobra"

"github.com/retr0h/osapi/internal/cli"
)

// clientNodeUserSSHKeyListCmd represents the user ssh-key list command.
var clientNodeUserSSHKeyListCmd = &cobra.Command{
Use: "list",
Short: "List SSH authorized keys",
Run: func(cmd *cobra.Command, _ []string) {
ctx := cmd.Context()
host, _ := cmd.Flags().GetString("target")
name, _ := cmd.Flags().GetString("name")

resp, err := sdkClient.User.ListKeys(ctx, host, name)
if err != nil {
cli.HandleError(err, logger)
return
}

if jsonOutput {
fmt.Println(string(resp.RawJSON()))
return
}

if resp.Data.JobID != "" {
fmt.Println()
cli.PrintKV("Job ID", resp.Data.JobID)
}

results := make([]cli.ResultRow, 0)
for _, r := range resp.Data.Results {
var errPtr *string
if r.Error != "" {
errPtr = &r.Error
}
if errPtr != nil || len(r.Keys) == 0 {
results = append(results, cli.ResultRow{
Hostname: r.Hostname,
Status: r.Status,
Error: errPtr,
Fields: []string{"", "", ""},
})

continue
}
for _, k := range r.Keys {
results = append(results, cli.ResultRow{
Hostname: r.Hostname,
Status: r.Status,
Fields: []string{
k.Type,
k.Fingerprint,
k.Comment,
},
})
}
}
headers, rows := cli.BuildBroadcastTable(results, []string{
"TYPE", "FINGERPRINT", "COMMENT",
})
cli.PrintCompactTable([]cli.Section{{Headers: headers, Rows: rows}})
},
}

func init() {
clientNodeUserSSHKeyCmd.AddCommand(clientNodeUserSSHKeyListCmd)

clientNodeUserSSHKeyListCmd.PersistentFlags().
String("name", "", "Username to list SSH keys for (required)")

_ = clientNodeUserSSHKeyListCmd.MarkPersistentFlagRequired("name")
}
87 changes: 87 additions & 0 deletions cmd/client_node_user_ssh_key_remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2026 John Dewey

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

package cmd

import (
"fmt"

"github.com/spf13/cobra"

"github.com/retr0h/osapi/internal/cli"
)

// clientNodeUserSSHKeyRemoveCmd represents the user ssh-key remove command.
var clientNodeUserSSHKeyRemoveCmd = &cobra.Command{
Use: "remove",
Short: "Remove an SSH authorized key",
Run: func(cmd *cobra.Command, _ []string) {
ctx := cmd.Context()
host, _ := cmd.Flags().GetString("target")
name, _ := cmd.Flags().GetString("name")
fingerprint, _ := cmd.Flags().GetString("fingerprint")

resp, err := sdkClient.User.RemoveKey(ctx, host, name, fingerprint)
if err != nil {
cli.HandleError(err, logger)
return
}

if jsonOutput {
fmt.Println(string(resp.RawJSON()))
return
}

if resp.Data.JobID != "" {
fmt.Println()
cli.PrintKV("Job ID", resp.Data.JobID)
}

results := make([]cli.MutationResultRow, 0, len(resp.Data.Results))
for _, r := range resp.Data.Results {
var errPtr *string
if r.Error != "" {
errPtr = &r.Error
}
changed := r.Changed
results = append(results, cli.MutationResultRow{
Hostname: r.Hostname,
Status: r.Status,
Changed: &changed,
Error: errPtr,
Fields: []string{fmt.Sprintf("%t", r.Changed)},
})
}
headers, rows := cli.BuildMutationTable(results, []string{"CHANGED"})
cli.PrintCompactTable([]cli.Section{{Headers: headers, Rows: rows}})
},
}

func init() {
clientNodeUserSSHKeyCmd.AddCommand(clientNodeUserSSHKeyRemoveCmd)

clientNodeUserSSHKeyRemoveCmd.PersistentFlags().
String("name", "", "Username to remove SSH key from (required)")
clientNodeUserSSHKeyRemoveCmd.PersistentFlags().
String("fingerprint", "", "Fingerprint of the SSH key to remove (required)")

_ = clientNodeUserSSHKeyRemoveCmd.MarkPersistentFlagRequired("name")
_ = clientNodeUserSSHKeyRemoveCmd.MarkPersistentFlagRequired("fingerprint")
}
Loading
Loading