Skip to content
Snippets Groups Projects
Commit de68c796 authored by Theo S Schult's avatar Theo S Schult
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
package cmd
import (
"github.com/spf13/cobra"
)
var createConfirm bool
var createCmd = &cobra.Command{
Use: "create",
Short: "Create resources",
}
func init() {
rootCmd.AddCommand(createCmd)
createCmd.PersistentFlags().BoolVarP(
&createConfirm,
"confirm",
"y",
false,
"Skip confirmation prompt",
)
}
package cmd
import (
"os"
"fmt"
"bufio"
"strings"
"github.com/spf13/cobra"
)
var groupName string
var createGroupCmd = &cobra.Command{
Use: "group",
Short: "Create a new group",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Groupname: %s\n", groupName)
// Confirmation logic
if !createConfirm {
fmt.Print("Proceed with group creation? [y/N] ")
reader := bufio.NewReader(os.Stdin)
response, _ := reader.ReadString('\n')
if strings.TrimSpace(strings.ToLower(response)) != "y" {
fmt.Println("Aborted.")
return
}
}
fmt.Println("Done!")
},
}
func init() {
createCmd.AddCommand(createGroupCmd)
// Possible Flags
createGroupCmd.Flags().StringVarP(&groupName, "groupname", "e", "", "Group name (use quotes for spaces)")
// Required Flags
createGroupCmd.MarkFlagRequired("groupname")
}
package cmd
import (
"os"
"fmt"
"bufio"
"strings"
"github.com/spf13/cobra"
)
var (
userGrr string
userLtype string
userPath string
userName string
userLogin string
userGroup string
userShell string
userHomedir string
userPassword string
)
var createUserCmd = &cobra.Command{
Use: "user",
Short: "Create a new user",
Run: func(cmd *cobra.Command, args []string) {
// Build user info string
userInfo := fmt.Sprintf(
`User Configuration:
Name: %s
Login: %s
Type: %s
Group: %s
Shell: %s
HomeDir: %s
Password: %s
WebPath: %s`,
userName,
ifThenElse(userLogin != "", userLogin, "[auto-generate]"),
userLtype,
userGroup,
userShell,
userHomedir,
ifThenElse(userPassword != "", "[set]", "[auto-generate]"),
userPath,
)
fmt.Println(userInfo)
// Confirmation logic
if !createConfirm {
fmt.Print("Proceed with user creation? [y/N] ")
reader := bufio.NewReader(os.Stdin)
response, _ := reader.ReadString('\n')
if strings.TrimSpace(strings.ToLower(response)) != "y" {
fmt.Println("Aborted.")
return
}
}
fmt.Println("Done!")
},
}
func init() {
createCmd.AddCommand(createUserCmd)
// Possible Flags
createUserCmd.Flags().StringVarP(&userGrr, "grr", "r", "", "User GRR, required for ini type")
createUserCmd.Flags().StringVarP(&userLtype, "type", "t", "ini", "Type of auto-generated login: ini, first or last")
createUserCmd.Flags().StringVarP(&userPath, "path", "w", "", "Path to webdir (e.g. path/login)")
createUserCmd.Flags().StringVarP(&userName, "name", "n", "", "User full name (use quotes for spaces)")
createUserCmd.Flags().StringVarP(&userLogin, "login", "l", "", "User login name (auto-generated if empty)")
createUserCmd.Flags().StringVarP(&userGroup, "group", "g", "", "User initial group")
createUserCmd.Flags().StringVarP(&userShell, "shell", "s", "/bin/bash", "Full path to shell ")
createUserCmd.Flags().StringVarP(&userHomedir, "homedir", "d", "", "Home directory path (/home/group/login if empty)")
createUserCmd.Flags().StringVarP(&userPassword, "password", "p", "", "User password (auto-generated if empty)")
// Required Flags
createUserCmd.MarkFlagRequired("name")
createUserCmd.MarkFlagRequired("type")
createUserCmd.MarkFlagRequired("group")
}
func ifThenElse(condition bool, a string, b string) string {
if condition {
return a
}
return b
}
package cmd
import (
"os"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "useradm",
Short: "A CLI to help administrate the user account in DINF",
Long: `This is a CLI for managing users in DINF UFPR made in Go
with the cobra library that took inspiration from the old
useradm.py. It takes care of LDAP configuration and was
made as an update for the previous version.`,
}
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
commands 0 → 100644
create
-user
-group
delete
-user
-group
modify
-user
-group
info
-user
-group
go.mod 0 → 100644
module gitlab.c3sl.ufpr.br/tss24/useradm
go 1.19
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
package main
import "gitlab.c3sl.ufpr.br/tss24/useradm/cmd"
func main() {
cmd.Execute()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment