Skip to content

Commit 7208138

Browse files
committed
create authctl cli tool
1 parent 698b2e0 commit 7208138

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

cmd/authctl/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/ubuntu/authd/cmd/authctl/user"
9+
)
10+
11+
const cmdName = "authctl"
12+
13+
var rootCmd = &cobra.Command{
14+
Use: fmt.Sprintf("%s COMMAND", cmdName),
15+
Short: "CLI tool to interact with authd services",
16+
Long: "authctl is a CLI tool which can be used to interact with various authd services.",
17+
Args: cobra.NoArgs,
18+
Run: func(cmd *cobra.Command, args []string) {},
19+
}
20+
21+
func init() {
22+
rootCmd.AddCommand(user.UserCmd)
23+
}
24+
25+
func main() {
26+
if err := rootCmd.Execute(); err != nil {
27+
fmt.Fprintf(os.Stderr, "Error while executing authctl '%s'\n", err)
28+
os.Exit(1)
29+
}
30+
}

cmd/authctl/user/disable.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package user
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/ubuntu/authd/internal/consts"
9+
"github.com/ubuntu/authd/internal/proto/authd"
10+
"google.golang.org/grpc"
11+
"google.golang.org/grpc/credentials/insecure"
12+
)
13+
14+
var DisableCmd = &cobra.Command{
15+
Use: "disable",
16+
Short: "Disable a user to log in",
17+
Args: cobra.ExactArgs(1),
18+
RunE: func(cmd *cobra.Command, args []string) error {
19+
fmt.Printf("Disabling user %s\n", args[0])
20+
conn, err := grpc.NewClient("unix://"+consts.DefaultSocketPath, grpc.WithTransportCredentials(insecure.NewCredentials()))
21+
22+
if err != nil {
23+
return err
24+
}
25+
26+
client := authd.NewNSSClient(conn)
27+
_, err = client.EnablePasswd(context.Background(), &authd.EnablePasswdRequest{Name: args[0]})
28+
29+
if err != nil {
30+
return err
31+
}
32+
33+
return nil
34+
},
35+
}

cmd/authctl/user/enable.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package user
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/ubuntu/authd/internal/consts"
9+
"github.com/ubuntu/authd/internal/proto/authd"
10+
"google.golang.org/grpc"
11+
"google.golang.org/grpc/credentials/insecure"
12+
)
13+
14+
var EnableCmd = &cobra.Command{
15+
Use: "enable",
16+
Short: "Enable a user to log in",
17+
Args: cobra.ExactArgs(1),
18+
RunE: func(cmd *cobra.Command, args []string) error {
19+
fmt.Printf("Enabling user %s\n", args[0])
20+
conn, err := grpc.NewClient("unix://"+consts.DefaultSocketPath, grpc.WithTransportCredentials(insecure.NewCredentials()))
21+
22+
if err != nil {
23+
return err
24+
}
25+
26+
client := authd.NewNSSClient(conn)
27+
_, err = client.EnablePasswd(context.Background(), &authd.EnablePasswdRequest{Name: args[0]})
28+
29+
if err != nil {
30+
return err
31+
}
32+
33+
return nil
34+
},
35+
}

cmd/authctl/user/user.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package user
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
var UserCmd = &cobra.Command{
8+
Use: "user",
9+
Short: "Commands retaled to working with users",
10+
Args: cobra.NoArgs,
11+
Run: func(cmd *cobra.Command, args []string) {},
12+
}
13+
14+
func init() {
15+
UserCmd.AddCommand(DisableCmd)
16+
UserCmd.AddCommand(EnableCmd)
17+
}

0 commit comments

Comments
 (0)