main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "os"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/util/shellutils"
  20. "yunion.io/x/structarg"
  21. _ "yunion.io/x/onecloud/cmd/ldapcli/shell"
  22. "yunion.io/x/onecloud/pkg/util/ldaputils"
  23. )
  24. type BaseOptions struct {
  25. Debug bool `help:"Show debug" default:"false"`
  26. Url string `help:"ldap url, like ldap://10.168.222.23:389 or ldaps://10.168.222.23:389" default:"$LDAP_URL"`
  27. Account string `help:"LDAP Account" default:"$LDAP_ACCOUNT"`
  28. Password string `help:"LDAP password" default:"$LDAP_PASSWORD"`
  29. BaseDN string `help:"LDAP base DN" default:"$LDAP_BASEDN"`
  30. SUBCOMMAND string `help:"aliyuncli subcommand" subcommand:"true"`
  31. }
  32. func getSubcommandParser() (*structarg.ArgumentParser, error) {
  33. parse, e := structarg.NewArgumentParserWithHelp(&BaseOptions{},
  34. "ldapcli",
  35. "Command-line tool for LDAP API.",
  36. `See "ldapcli COMMAND --help" for help on a specific command.`)
  37. if e != nil {
  38. return nil, e
  39. }
  40. subcmd := parse.GetSubcommand()
  41. if subcmd == nil {
  42. return nil, fmt.Errorf("No subcommand argument.")
  43. }
  44. for _, v := range shellutils.CommandTable {
  45. _, e := subcmd.AddSubParserWithHelp(v.Options, v.Command, v.Desc, v.Callback)
  46. if e != nil {
  47. return nil, e
  48. }
  49. }
  50. return parse, nil
  51. }
  52. func showErrorAndExit(e error) {
  53. fmt.Fprintf(os.Stderr, "%s", e)
  54. fmt.Fprintln(os.Stderr)
  55. os.Exit(1)
  56. }
  57. func newClient(options *BaseOptions) (*ldaputils.SLDAPClient, error) {
  58. if len(options.Url) == 0 {
  59. return nil, fmt.Errorf("Missing ldap URL")
  60. }
  61. if len(options.BaseDN) == 0 {
  62. return nil, fmt.Errorf("Missing BaseDN, e.g. DC=example,DC=com")
  63. }
  64. cli := ldaputils.NewLDAPClient(options.Url, options.Account, options.Password, options.BaseDN, options.Debug)
  65. err := cli.Connect()
  66. if err != nil {
  67. log.Errorf("connect fail %s", err)
  68. return nil, err
  69. }
  70. return cli, nil
  71. }
  72. func main() {
  73. parser, e := getSubcommandParser()
  74. if e != nil {
  75. showErrorAndExit(e)
  76. }
  77. e = parser.ParseArgs(os.Args[1:], false)
  78. options := parser.Options().(*BaseOptions)
  79. if parser.IsHelpSet() {
  80. fmt.Print(parser.HelpString())
  81. return
  82. }
  83. subcmd := parser.GetSubcommand()
  84. subparser := subcmd.GetSubParser()
  85. if e != nil || subparser == nil {
  86. if subparser != nil {
  87. fmt.Print(subparser.Usage())
  88. } else {
  89. fmt.Print(parser.Usage())
  90. }
  91. showErrorAndExit(e)
  92. }
  93. var cli *ldaputils.SLDAPClient
  94. suboptions := subparser.Options()
  95. if subparser.IsHelpSet() {
  96. fmt.Print(subparser.HelpString())
  97. return
  98. }
  99. cli, e = newClient(options)
  100. if e != nil {
  101. showErrorAndExit(e)
  102. }
  103. e = subcmd.Invoke(cli, suboptions)
  104. if e != nil {
  105. showErrorAndExit(e)
  106. }
  107. }