Skip to content

Commit

Permalink
Merge pull request #3 from 47oo/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
47oo authored Jun 15, 2022
2 parents df20463 + 0abf780 commit f76eb4a
Show file tree
Hide file tree
Showing 21 changed files with 119 additions and 201 deletions.
16 changes: 6 additions & 10 deletions cmd/groupadd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ import (
"github.com/spf13/cobra"
)

var groupaddName string
var groupaddGidNumber string
var groupaddDesc string
var groupaddTeamName string

func groupaddRun() {
func groupaddRun(cmd *cobra.Command, args []string) {

o := eldap.NewOption()
g := eldap.NewGroupEntry()
g.Name = append(g.Name, groupaddName)
g.Name = args
g.Description = append(g.Description, groupaddDesc)
g.GidNumber = append(g.GidNumber, groupaddGidNumber)
if err := o.GroupAdd(groupaddTeamName, g); err != nil {
Expand All @@ -42,22 +41,19 @@ func groupaddRun() {

// groupaddCmd represents the groupadd command
var groupaddCmd = &cobra.Command{
Use: "groupadd",
Use: "groupadd [flags] GROUP",
Short: "create a group",
Long: `The groupadd command creates a new group account using the values specified on the command line plus the default values from
the system. The new group will be entered into the ldap server as needed.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
groupaddRun()
groupaddRun(cmd, args)
},
}

func init() {
rootCmd.AddCommand(groupaddCmd)
groupaddCmd.Flags().StringVarP(&groupaddGidNumber, "gid", "g", "", "use GID for the new group")
groupaddCmd.Flags().StringVarP(&groupaddName, "name", "n", "", "Group Name")
groupaddCmd.Flags().StringVarP(&groupaddDesc, "desc", "d", "no_desc", "Group Description")
groupaddCmd.Flags().StringVarP(&groupaddDesc, "desc", "d", "", "Group Description")
groupaddCmd.Flags().StringVarP(&groupaddTeamName, "teamname", "t", "", "You want the group in which team, or default team")
groupaddCmd.MarkFlagRequired("name")
groupaddCmd.MarkFlagRequired("gid")

}
16 changes: 5 additions & 11 deletions cmd/groupdel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,28 @@ package cmd

import (
"ela/eldap"
"ela/model"
"log"

"github.com/spf13/cobra"
)

var groupdelInfo = model.GroupInfo{}

func groupdelRun() {
func groupdelRun(cmd *cobra.Command, args []string) {
o := eldap.NewOption()
if err := o.GroupDel(groupdelInfo.Name); err != nil {
if err := o.GroupDel(args[0]); err != nil {
log.Fatalln(err)
}
}

// groupdelCmd represents the groupdel command
var groupdelCmd = &cobra.Command{
Use: "groupdel",
Use: "groupdel [flags] GROUP",
Short: "groupdel - delete a group",
Long: `The groupdel command modifies the system account files, deleting all entries that refer to GROUP. The named group must exist.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
groupdelRun()
groupdelRun(cmd, args)
},
}

func init() {
rootCmd.AddCommand(groupdelCmd)
groupdelCmd.Flags().StringVarP(&groupdelInfo.Name, "name", "n", "", "Group Name")
groupdelCmd.MarkFlagRequired("name")

}
9 changes: 1 addition & 8 deletions cmd/groupmems.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@ import (
var groupmemsName string
var addUserName string
var delUserName string
var groupmemsList bool

func groupmemsRun() {
o := eldap.NewOption()
if groupmemsList {
log.Fatalln("not support now")
return
}

if addUserName != "" {
if err := o.GroupMems(groupmemsName, []string{addUserName}, eldap.Add); err != nil {
log.Fatalln(err)
Expand Down Expand Up @@ -64,8 +60,5 @@ func init() {
groupmemsCmd.Flags().StringVarP(&groupmemsName, "group", "g", "", "change groupname instead of the user's group")
groupmemsCmd.Flags().StringVarP(&addUserName, "add", "a", "", "add username to the members of the group")
groupmemsCmd.Flags().StringVarP(&delUserName, "delete", "d", "", "add username to the members of the group")
groupmemsCmd.Flags().BoolVarP(&groupmemsList, "list", "l", false, "list the members of the group")

groupmemsCmd.MarkFlagRequired("group")

}
15 changes: 6 additions & 9 deletions cmd/groupmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ import (
"github.com/spf13/cobra"
)

var groupmodName string
var groupmodGidNumber string
var groupmodDesc string

func groupmodRun() {
func groupmodRun(cmd *cobra.Command, args []string) {
o := eldap.NewOption()
if groupaddGidNumber != "" {
if err := o.GroupMod(groupmodName, groupmodGidNumber); err != nil {
if groupmodGidNumber != "" {
if err := o.GroupMod(args[0], groupmodGidNumber); err != nil {
log.Fatalln(err)
}
}
Expand All @@ -38,19 +37,17 @@ func groupmodRun() {

// groupmodCmd represents the groupmod command
var groupmodCmd = &cobra.Command{
Use: "groupmod",
Use: "groupmod [flags] GROUP",
Short: "modify a group definition on the system",
Long: `The groupmod command modifies the definition of the specified GROUP by modifying the appropriate entry in the group database.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
groupmodRun()
groupmodRun(cmd, args)
},
}

func init() {
rootCmd.AddCommand(groupmodCmd)
groupmodCmd.Flags().StringVarP(&groupmodName, "name", "n", "", "groupname")
groupmodCmd.Flags().StringVarP(&groupmodGidNumber, "gid", "g", "", "change the group ID to GID")
groupmodCmd.Flags().StringVarP(&groupmodDesc, "desc", "d", "", "Descroption Not support now")
groupmodCmd.MarkFlagRequired("name")

}
8 changes: 2 additions & 6 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ func initRun() {
fmt.Printf("Please enter ldap Admin account: ")
fmt.Scanln(&lai.Admin)
pd, _ := gopass.GetPasswdPrompt(`Please enter ldap admin passwd(enter "NO" to not write password): `, true, os.Stdin, os.Stdout)
asepd, err := secret.EncryptAES([]byte(pd), secret.KEY)
if err != nil {
log.Fatalln(err)
return
}
asepd := secret.EasyEncrypt([]byte(pd), secret.KEY)

lai.AdminPW = string(asepd)
homedir, _ := os.UserHomeDir()
Expand All @@ -55,7 +51,7 @@ func initRun() {
dS.NewKey("Admin", lai.Admin)
dS.NewKey("AdminPW", lai.AdminPW)
dS.NewKey("TopDN", lai.TopDN)
if err = cfg.SaveTo(filename); err != nil {
if err := cfg.SaveTo(filename); err != nil {
log.Fatalln(err)
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -87,7 +88,8 @@ func initConfig() {
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
if err := viper.ReadInConfig(); err != nil {
// fmt.Println("Using config file:", viper.ConfigFileUsed())
log.Fatalln("Using config file Error:", viper.ConfigFileUsed())
}
}
19 changes: 7 additions & 12 deletions cmd/teamadd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (
"github.com/spf13/cobra"
)

var teamaddName string
var teamaddDesc string

func teamaddRun() {
func teamaddRun(cmd *cobra.Command, args []string) {

o := eldap.NewOption()
t := eldap.NewTeamEntry()
t.Name = append(t.Name, teamaddName)
t.Name = args
t.Description = append(t.Description, teamaddDesc)
if err := o.TeamAdd(t); err != nil {
log.Fatalln(err)
Expand All @@ -38,21 +38,16 @@ func teamaddRun() {

// teamaddCmd represents the teamadd command
var teamaddCmd = &cobra.Command{
Use: "teamadd",
Use: "teamadd [flags] TEAM",
Short: "create a new team",
Long: `team is an organization in ldap. For example:
ela teamadd -n <teamname> [-d <descprition>]`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
teamaddRun()
teamaddRun(cmd, args)
},
}

func init() {
rootCmd.AddCommand(teamaddCmd)

teamaddCmd.Flags().StringVarP(&teamaddName, "name", "n", "", "Team Name you create")
teamaddCmd.Flags().StringVarP(&teamaddDesc, "desc", "d", "no_desc", "Team Description")
teamaddCmd.MarkFlagRequired("name")
teamaddCmd.Flags().StringVarP(&teamaddDesc, "desc", "d", "", "Team Description")

}
19 changes: 6 additions & 13 deletions cmd/teamdel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,29 @@ package cmd

import (
"ela/eldap"
"ela/model"
"log"

"github.com/spf13/cobra"
)

func teamdelRun() {
func teamdelRun(cmd *cobra.Command, args []string) {
o := eldap.NewOption()
if teamdelInfo.Name == "" {
log.Fatalln("Team Name Must Exist")
}
if err := o.TeamDelete(teamdelInfo.Name); err != nil {

if err := o.TeamDelete(args[0]); err != nil {
log.Fatalln(err)
}
}

// teamdelCmd represents the teamdel command
var teamdelCmd = &cobra.Command{
Use: "teamdel",
Use: "teamdel [flags] TEAM",
Short: "delete a user account",
Long: `delete a user account`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
teamdelRun()
teamdelRun(cmd, args)
},
}
var teamdelInfo = model.TeamInfo{}

func init() {
rootCmd.AddCommand(teamdelCmd)
teamdelCmd.Flags().StringVarP(&teamdelInfo.Name, "name", "n", "", "The team you want to del")
teamdelCmd.MarkFlagRequired("name")

}
13 changes: 5 additions & 8 deletions cmd/teammod.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ import (
"github.com/spf13/cobra"
)

var teammodName string
var teammodDesc string

func teammodRun() {
func teammodRun(cmd *cobra.Command, args []string) {
o := eldap.NewOption()
t := eldap.NewTeamEntry()
t.Name = append(t.Name, teammodName)
t.Name = args
t.Description = append(t.Description, teammodDesc)
if err := o.TeamDescUpdate(t); err != nil {
log.Fatalln(err)
Expand All @@ -37,19 +36,17 @@ func teammodRun() {

// teammodCmd represents the teammod command
var teammodCmd = &cobra.Command{
Use: "teammod",
Use: "teammod [flags] TEAM",
Short: "modify a team",
Long: `modify a team,only support modify desc`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
teammodRun()
teammodRun(cmd, args)
},
}

func init() {
rootCmd.AddCommand(teammodCmd)
teammodCmd.Flags().StringVarP(&teammodName, "name", "n", "", "Team Name")
teammodCmd.Flags().StringVarP(&teammodDesc, "desc", "d", "", "Team Description")
teammodCmd.MarkFlagRequired("name")
teammodCmd.MarkFlagRequired("desc")

}
20 changes: 8 additions & 12 deletions cmd/useradd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,27 @@ import (
"github.com/spf13/cobra"
)

var useraddName string
var useraddHomeDirectory string
var useraddGidNumber string
var useraddUidNumber string
var useraddUserPassword string
var useraddLoginShell string
var useraddTeamName string

func useraddRun() {
func useraddRun(cmd *cobra.Command, args []string) {
if useraddHomeDirectory == "" {
useraddHomeDirectory = fmt.Sprintf(`/%s/%s`, "home", useraddName)
useraddHomeDirectory = fmt.Sprintf(`/%s/%s`, "home", args[0])
}
if useraddUserPassword == "" {
useraddUserPassword = useraddName
useraddUserPassword = args[0]
}
if useraddLoginShell == "" {
useraddLoginShell = "/bin/bash"
}
o := eldap.NewOption()
u := eldap.NewUserEntry()
u.CN = append(u.CN, useraddName)
u.Name = append(u.Name, useraddName)
u.CN = args
u.Name = args
u.GidNumber = append(u.GidNumber, useraddGidNumber)
u.UidNumber = append(u.UidNumber, useraddUidNumber)
u.HomeDirectory = append(u.HomeDirectory, useraddHomeDirectory)
Expand All @@ -58,26 +57,23 @@ func useraddRun() {

// useraddCmd represents the useradd command
var useraddCmd = &cobra.Command{
Use: "useradd",
Use: "useradd [flags] LOGIN",
Short: "create a new user or update default new user information",
Long: `the useradd command creates a new user account using the values specified on the command
line plus the default values from the ldap`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
useraddRun()
useraddRun(cmd, args)
},
}

func init() {
rootCmd.AddCommand(useraddCmd)
useraddCmd.Flags().StringVarP(&useraddName, "name", "n", "", "username")
useraddCmd.Flags().StringVarP(&useraddHomeDirectory, "home-dir", "d", "", "user home dir")
useraddCmd.Flags().StringVarP(&useraddGidNumber, "gid", "g", "", "group number")
useraddCmd.Flags().StringVarP(&useraddUidNumber, "uid", "u", "", "user uid number")
useraddCmd.Flags().StringVarP(&useraddUserPassword, "password", "p", "", "encrypted password of the new account")
useraddCmd.Flags().StringVarP(&useraddLoginShell, "shell", "s", "", "login shell of the new account")
useraddCmd.Flags().StringVarP(&useraddTeamName, "team", "t", "", "teamname for this user")

useraddCmd.MarkFlagRequired("name")
useraddCmd.MarkFlagRequired("gid")
useraddCmd.MarkFlagRequired("uid")

Expand Down
Loading

0 comments on commit f76eb4a

Please sign in to comment.