You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin.go 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package cmd
  6. import (
  7. "context"
  8. "errors"
  9. "fmt"
  10. "os"
  11. "strings"
  12. "text/tabwriter"
  13. "code.gitea.io/gitea/models"
  14. "code.gitea.io/gitea/modules/auth/oauth2"
  15. "code.gitea.io/gitea/modules/git"
  16. "code.gitea.io/gitea/modules/graceful"
  17. "code.gitea.io/gitea/modules/log"
  18. pwd "code.gitea.io/gitea/modules/password"
  19. repo_module "code.gitea.io/gitea/modules/repository"
  20. "code.gitea.io/gitea/modules/setting"
  21. "github.com/urfave/cli"
  22. )
  23. var (
  24. // CmdAdmin represents the available admin sub-command.
  25. CmdAdmin = cli.Command{
  26. Name: "admin",
  27. Usage: "Command line interface to perform common administrative operations",
  28. Subcommands: []cli.Command{
  29. subcmdUser,
  30. subcmdRepoSyncReleases,
  31. subcmdRegenerate,
  32. subcmdAuth,
  33. subcmdSendMail,
  34. },
  35. }
  36. subcmdUser = cli.Command{
  37. Name: "user",
  38. Usage: "Modify users",
  39. Subcommands: []cli.Command{
  40. microcmdUserCreate,
  41. microcmdUserList,
  42. microcmdUserChangePassword,
  43. microcmdUserDelete,
  44. },
  45. }
  46. microcmdUserList = cli.Command{
  47. Name: "list",
  48. Usage: "List users",
  49. Action: runListUsers,
  50. Flags: []cli.Flag{
  51. cli.BoolFlag{
  52. Name: "admin",
  53. Usage: "List only admin users",
  54. },
  55. },
  56. }
  57. microcmdUserCreate = cli.Command{
  58. Name: "create",
  59. Usage: "Create a new user in database",
  60. Action: runCreateUser,
  61. Flags: []cli.Flag{
  62. cli.StringFlag{
  63. Name: "name",
  64. Usage: "Username. DEPRECATED: use username instead",
  65. },
  66. cli.StringFlag{
  67. Name: "username",
  68. Usage: "Username",
  69. },
  70. cli.StringFlag{
  71. Name: "password",
  72. Usage: "User password",
  73. },
  74. cli.StringFlag{
  75. Name: "email",
  76. Usage: "User email address",
  77. },
  78. cli.BoolFlag{
  79. Name: "admin",
  80. Usage: "User is an admin",
  81. },
  82. cli.BoolFlag{
  83. Name: "random-password",
  84. Usage: "Generate a random password for the user",
  85. },
  86. cli.BoolFlag{
  87. Name: "must-change-password",
  88. Usage: "Set this option to false to prevent forcing the user to change their password after initial login, (Default: true)",
  89. },
  90. cli.IntFlag{
  91. Name: "random-password-length",
  92. Usage: "Length of the random password to be generated",
  93. Value: 12,
  94. },
  95. cli.BoolFlag{
  96. Name: "access-token",
  97. Usage: "Generate access token for the user",
  98. },
  99. },
  100. }
  101. microcmdUserChangePassword = cli.Command{
  102. Name: "change-password",
  103. Usage: "Change a user's password",
  104. Action: runChangePassword,
  105. Flags: []cli.Flag{
  106. cli.StringFlag{
  107. Name: "username,u",
  108. Value: "",
  109. Usage: "The user to change password for",
  110. },
  111. cli.StringFlag{
  112. Name: "password,p",
  113. Value: "",
  114. Usage: "New password to set for user",
  115. },
  116. },
  117. }
  118. microcmdUserDelete = cli.Command{
  119. Name: "delete",
  120. Usage: "Delete specific user by id, name or email",
  121. Flags: []cli.Flag{
  122. cli.Int64Flag{
  123. Name: "id",
  124. Usage: "ID of user of the user to delete",
  125. },
  126. cli.StringFlag{
  127. Name: "username,u",
  128. Usage: "Username of the user to delete",
  129. },
  130. cli.StringFlag{
  131. Name: "email,e",
  132. Usage: "Email of the user to delete",
  133. },
  134. },
  135. Action: runDeleteUser,
  136. }
  137. subcmdRepoSyncReleases = cli.Command{
  138. Name: "repo-sync-releases",
  139. Usage: "Synchronize repository releases with tags",
  140. Action: runRepoSyncReleases,
  141. }
  142. subcmdRegenerate = cli.Command{
  143. Name: "regenerate",
  144. Usage: "Regenerate specific files",
  145. Subcommands: []cli.Command{
  146. microcmdRegenHooks,
  147. microcmdRegenKeys,
  148. },
  149. }
  150. microcmdRegenHooks = cli.Command{
  151. Name: "hooks",
  152. Usage: "Regenerate git-hooks",
  153. Action: runRegenerateHooks,
  154. }
  155. microcmdRegenKeys = cli.Command{
  156. Name: "keys",
  157. Usage: "Regenerate authorized_keys file",
  158. Action: runRegenerateKeys,
  159. }
  160. subcmdAuth = cli.Command{
  161. Name: "auth",
  162. Usage: "Modify external auth providers",
  163. Subcommands: []cli.Command{
  164. microcmdAuthAddOauth,
  165. microcmdAuthUpdateOauth,
  166. cmdAuthAddLdapBindDn,
  167. cmdAuthUpdateLdapBindDn,
  168. cmdAuthAddLdapSimpleAuth,
  169. cmdAuthUpdateLdapSimpleAuth,
  170. microcmdAuthList,
  171. microcmdAuthDelete,
  172. },
  173. }
  174. microcmdAuthList = cli.Command{
  175. Name: "list",
  176. Usage: "List auth sources",
  177. Action: runListAuth,
  178. Flags: []cli.Flag{
  179. cli.IntFlag{
  180. Name: "min-width",
  181. Usage: "Minimal cell width including any padding for the formatted table",
  182. Value: 0,
  183. },
  184. cli.IntFlag{
  185. Name: "tab-width",
  186. Usage: "width of tab characters in formatted table (equivalent number of spaces)",
  187. Value: 8,
  188. },
  189. cli.IntFlag{
  190. Name: "padding",
  191. Usage: "padding added to a cell before computing its width",
  192. Value: 1,
  193. },
  194. cli.StringFlag{
  195. Name: "pad-char",
  196. Usage: `ASCII char used for padding if padchar == '\\t', the Writer will assume that the width of a '\\t' in the formatted output is tabwidth, and cells are left-aligned independent of align_left (for correct-looking results, tabwidth must correspond to the tab width in the viewer displaying the result)`,
  197. Value: "\t",
  198. },
  199. cli.BoolFlag{
  200. Name: "vertical-bars",
  201. Usage: "Set to true to print vertical bars between columns",
  202. },
  203. },
  204. }
  205. idFlag = cli.Int64Flag{
  206. Name: "id",
  207. Usage: "ID of authentication source",
  208. }
  209. microcmdAuthDelete = cli.Command{
  210. Name: "delete",
  211. Usage: "Delete specific auth source",
  212. Flags: []cli.Flag{idFlag},
  213. Action: runDeleteAuth,
  214. }
  215. oauthCLIFlags = []cli.Flag{
  216. cli.StringFlag{
  217. Name: "name",
  218. Value: "",
  219. Usage: "Application Name",
  220. },
  221. cli.StringFlag{
  222. Name: "provider",
  223. Value: "",
  224. Usage: "OAuth2 Provider",
  225. },
  226. cli.StringFlag{
  227. Name: "key",
  228. Value: "",
  229. Usage: "Client ID (Key)",
  230. },
  231. cli.StringFlag{
  232. Name: "secret",
  233. Value: "",
  234. Usage: "Client Secret",
  235. },
  236. cli.StringFlag{
  237. Name: "auto-discover-url",
  238. Value: "",
  239. Usage: "OpenID Connect Auto Discovery URL (only required when using OpenID Connect as provider)",
  240. },
  241. cli.StringFlag{
  242. Name: "use-custom-urls",
  243. Value: "false",
  244. Usage: "Use custom URLs for GitLab/GitHub OAuth endpoints",
  245. },
  246. cli.StringFlag{
  247. Name: "custom-auth-url",
  248. Value: "",
  249. Usage: "Use a custom Authorization URL (option for GitLab/GitHub)",
  250. },
  251. cli.StringFlag{
  252. Name: "custom-token-url",
  253. Value: "",
  254. Usage: "Use a custom Token URL (option for GitLab/GitHub)",
  255. },
  256. cli.StringFlag{
  257. Name: "custom-profile-url",
  258. Value: "",
  259. Usage: "Use a custom Profile URL (option for GitLab/GitHub)",
  260. },
  261. cli.StringFlag{
  262. Name: "custom-email-url",
  263. Value: "",
  264. Usage: "Use a custom Email URL (option for GitHub)",
  265. },
  266. cli.StringFlag{
  267. Name: "icon-url",
  268. Value: "",
  269. Usage: "Custom icon URL for OAuth2 login source",
  270. },
  271. }
  272. microcmdAuthUpdateOauth = cli.Command{
  273. Name: "update-oauth",
  274. Usage: "Update existing Oauth authentication source",
  275. Action: runUpdateOauth,
  276. Flags: append(oauthCLIFlags[:1], append([]cli.Flag{idFlag}, oauthCLIFlags[1:]...)...),
  277. }
  278. microcmdAuthAddOauth = cli.Command{
  279. Name: "add-oauth",
  280. Usage: "Add new Oauth authentication source",
  281. Action: runAddOauth,
  282. Flags: oauthCLIFlags,
  283. }
  284. subcmdSendMail = cli.Command{
  285. Name: "sendmail",
  286. Usage: "Send a message to all users",
  287. Action: runSendMail,
  288. Flags: []cli.Flag{
  289. cli.StringFlag{
  290. Name: "title",
  291. Usage: `a title of a message`,
  292. Value: "",
  293. },
  294. cli.StringFlag{
  295. Name: "content",
  296. Usage: "a content of a message",
  297. Value: "",
  298. },
  299. cli.BoolFlag{
  300. Name: "force,f",
  301. Usage: "A flag to bypass a confirmation step",
  302. },
  303. },
  304. }
  305. )
  306. func runChangePassword(c *cli.Context) error {
  307. if err := argsSet(c, "username", "password"); err != nil {
  308. return err
  309. }
  310. if err := initDB(); err != nil {
  311. return err
  312. }
  313. if !pwd.IsComplexEnough(c.String("password")) {
  314. return errors.New("Password does not meet complexity requirements")
  315. }
  316. pwned, err := pwd.IsPwned(context.Background(), c.String("password"))
  317. if err != nil {
  318. return err
  319. }
  320. if pwned {
  321. return errors.New("The password you chose is on a list of stolen passwords previously exposed in public data breaches. Please try again with a different password.\nFor more details, see https://haveibeenpwned.com/Passwords")
  322. }
  323. uname := c.String("username")
  324. user, err := models.GetUserByName(uname)
  325. if err != nil {
  326. return err
  327. }
  328. if user.Salt, err = models.GetUserSalt(); err != nil {
  329. return err
  330. }
  331. user.HashPassword(c.String("password"))
  332. if err := models.UpdateUserCols(user, "passwd", "passwd_hash_algo", "salt"); err != nil {
  333. return err
  334. }
  335. fmt.Printf("%s's password has been successfully updated!\n", user.Name)
  336. return nil
  337. }
  338. func runCreateUser(c *cli.Context) error {
  339. if err := argsSet(c, "email"); err != nil {
  340. return err
  341. }
  342. if c.IsSet("name") && c.IsSet("username") {
  343. return errors.New("Cannot set both --name and --username flags")
  344. }
  345. if !c.IsSet("name") && !c.IsSet("username") {
  346. return errors.New("One of --name or --username flags must be set")
  347. }
  348. if c.IsSet("password") && c.IsSet("random-password") {
  349. return errors.New("cannot set both -random-password and -password flags")
  350. }
  351. var username string
  352. if c.IsSet("username") {
  353. username = c.String("username")
  354. } else {
  355. username = c.String("name")
  356. fmt.Fprintf(os.Stderr, "--name flag is deprecated. Use --username instead.\n")
  357. }
  358. if err := initDB(); err != nil {
  359. return err
  360. }
  361. var password string
  362. if c.IsSet("password") {
  363. password = c.String("password")
  364. } else if c.IsSet("random-password") {
  365. var err error
  366. password, err = pwd.Generate(c.Int("random-password-length"))
  367. if err != nil {
  368. return err
  369. }
  370. fmt.Printf("generated random password is '%s'\n", password)
  371. } else {
  372. return errors.New("must set either password or random-password flag")
  373. }
  374. // always default to true
  375. var changePassword = true
  376. // If this is the first user being created.
  377. // Take it as the admin and don't force a password update.
  378. if n := models.CountUsers(); n == 0 {
  379. changePassword = false
  380. }
  381. if c.IsSet("must-change-password") {
  382. changePassword = c.Bool("must-change-password")
  383. }
  384. u := &models.User{
  385. Name: username,
  386. Email: c.String("email"),
  387. Passwd: password,
  388. IsActive: true,
  389. IsAdmin: c.Bool("admin"),
  390. MustChangePassword: changePassword,
  391. Theme: setting.UI.DefaultTheme,
  392. }
  393. if err := models.CreateUser(u); err != nil {
  394. return fmt.Errorf("CreateUser: %v", err)
  395. }
  396. if c.Bool("access-token") {
  397. t := &models.AccessToken{
  398. Name: "gitea-admin",
  399. UID: u.ID,
  400. }
  401. if err := models.NewAccessToken(t); err != nil {
  402. return err
  403. }
  404. fmt.Printf("Access token was successfully created... %s\n", t.Token)
  405. }
  406. fmt.Printf("New user '%s' has been successfully created!\n", username)
  407. return nil
  408. }
  409. func runListUsers(c *cli.Context) error {
  410. if err := initDB(); err != nil {
  411. return err
  412. }
  413. users, err := models.GetAllUsers()
  414. if err != nil {
  415. return err
  416. }
  417. w := tabwriter.NewWriter(os.Stdout, 5, 0, 1, ' ', 0)
  418. if c.IsSet("admin") {
  419. fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\n")
  420. for _, u := range users {
  421. if u.IsAdmin {
  422. fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", u.ID, u.Name, u.Email, u.IsActive)
  423. }
  424. }
  425. } else {
  426. fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\tIsAdmin\n")
  427. for _, u := range users {
  428. fmt.Fprintf(w, "%d\t%s\t%s\t%t\t%t\n", u.ID, u.Name, u.Email, u.IsActive, u.IsAdmin)
  429. }
  430. }
  431. w.Flush()
  432. return nil
  433. }
  434. func runDeleteUser(c *cli.Context) error {
  435. if !c.IsSet("id") && !c.IsSet("username") && !c.IsSet("email") {
  436. return fmt.Errorf("You must provide the id, username or email of a user to delete")
  437. }
  438. if err := initDB(); err != nil {
  439. return err
  440. }
  441. var err error
  442. var user *models.User
  443. if c.IsSet("email") {
  444. user, err = models.GetUserByEmail(c.String("email"))
  445. } else if c.IsSet("username") {
  446. user, err = models.GetUserByName(c.String("username"))
  447. } else {
  448. user, err = models.GetUserByID(c.Int64("id"))
  449. }
  450. if err != nil {
  451. return err
  452. }
  453. if c.IsSet("username") && user.LowerName != strings.ToLower(strings.TrimSpace(c.String("username"))) {
  454. return fmt.Errorf("The user %s who has email %s does not match the provided username %s", user.Name, c.String("email"), c.String("username"))
  455. }
  456. if c.IsSet("id") && user.ID != c.Int64("id") {
  457. return fmt.Errorf("The user %s does not match the provided id %d", user.Name, c.Int64("id"))
  458. }
  459. return models.DeleteUser(user)
  460. }
  461. func runRepoSyncReleases(c *cli.Context) error {
  462. if err := initDB(); err != nil {
  463. return err
  464. }
  465. log.Trace("Synchronizing repository releases (this may take a while)")
  466. for page := 1; ; page++ {
  467. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  468. ListOptions: models.ListOptions{
  469. PageSize: models.RepositoryListDefaultPageSize,
  470. Page: page,
  471. },
  472. Private: true,
  473. })
  474. if err != nil {
  475. return fmt.Errorf("SearchRepositoryByName: %v", err)
  476. }
  477. if len(repos) == 0 {
  478. break
  479. }
  480. log.Trace("Processing next %d repos of %d", len(repos), count)
  481. for _, repo := range repos {
  482. log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath())
  483. gitRepo, err := git.OpenRepository(repo.RepoPath())
  484. if err != nil {
  485. log.Warn("OpenRepository: %v", err)
  486. continue
  487. }
  488. oldnum, err := getReleaseCount(repo.ID)
  489. if err != nil {
  490. log.Warn(" GetReleaseCountByRepoID: %v", err)
  491. }
  492. log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)
  493. if err = repo_module.SyncReleasesWithTags(repo, gitRepo); err != nil {
  494. log.Warn(" SyncReleasesWithTags: %v", err)
  495. gitRepo.Close()
  496. continue
  497. }
  498. count, err = getReleaseCount(repo.ID)
  499. if err != nil {
  500. log.Warn(" GetReleaseCountByRepoID: %v", err)
  501. gitRepo.Close()
  502. continue
  503. }
  504. log.Trace(" repo %s releases synchronized to tags: from %d to %d",
  505. repo.FullName(), oldnum, count)
  506. gitRepo.Close()
  507. }
  508. }
  509. return nil
  510. }
  511. func getReleaseCount(id int64) (int64, error) {
  512. return models.GetReleaseCountByRepoID(
  513. id,
  514. models.FindReleasesOptions{
  515. IncludeTags: true,
  516. },
  517. )
  518. }
  519. func runRegenerateHooks(c *cli.Context) error {
  520. if err := initDB(); err != nil {
  521. return err
  522. }
  523. return repo_module.SyncRepositoryHooks(graceful.GetManager().ShutdownContext())
  524. }
  525. func runRegenerateKeys(c *cli.Context) error {
  526. if err := initDB(); err != nil {
  527. return err
  528. }
  529. return models.RewriteAllPublicKeys()
  530. }
  531. func parseOAuth2Config(c *cli.Context) *models.OAuth2Config {
  532. var customURLMapping *oauth2.CustomURLMapping
  533. if c.IsSet("use-custom-urls") {
  534. customURLMapping = &oauth2.CustomURLMapping{
  535. TokenURL: c.String("custom-token-url"),
  536. AuthURL: c.String("custom-auth-url"),
  537. ProfileURL: c.String("custom-profile-url"),
  538. EmailURL: c.String("custom-email-url"),
  539. }
  540. } else {
  541. customURLMapping = nil
  542. }
  543. return &models.OAuth2Config{
  544. Provider: c.String("provider"),
  545. ClientID: c.String("key"),
  546. ClientSecret: c.String("secret"),
  547. OpenIDConnectAutoDiscoveryURL: c.String("auto-discover-url"),
  548. CustomURLMapping: customURLMapping,
  549. IconURL: c.String("icon-url"),
  550. }
  551. }
  552. func runAddOauth(c *cli.Context) error {
  553. if err := initDB(); err != nil {
  554. return err
  555. }
  556. return models.CreateLoginSource(&models.LoginSource{
  557. Type: models.LoginOAuth2,
  558. Name: c.String("name"),
  559. IsActived: true,
  560. Cfg: parseOAuth2Config(c),
  561. })
  562. }
  563. func runUpdateOauth(c *cli.Context) error {
  564. if !c.IsSet("id") {
  565. return fmt.Errorf("--id flag is missing")
  566. }
  567. if err := initDB(); err != nil {
  568. return err
  569. }
  570. source, err := models.GetLoginSourceByID(c.Int64("id"))
  571. if err != nil {
  572. return err
  573. }
  574. oAuth2Config := source.OAuth2()
  575. if c.IsSet("name") {
  576. source.Name = c.String("name")
  577. }
  578. if c.IsSet("provider") {
  579. oAuth2Config.Provider = c.String("provider")
  580. }
  581. if c.IsSet("key") {
  582. oAuth2Config.ClientID = c.String("key")
  583. }
  584. if c.IsSet("secret") {
  585. oAuth2Config.ClientSecret = c.String("secret")
  586. }
  587. if c.IsSet("auto-discover-url") {
  588. oAuth2Config.OpenIDConnectAutoDiscoveryURL = c.String("auto-discover-url")
  589. }
  590. if c.IsSet("icon-url") {
  591. oAuth2Config.IconURL = c.String("icon-url")
  592. }
  593. // update custom URL mapping
  594. var customURLMapping = &oauth2.CustomURLMapping{}
  595. if oAuth2Config.CustomURLMapping != nil {
  596. customURLMapping.TokenURL = oAuth2Config.CustomURLMapping.TokenURL
  597. customURLMapping.AuthURL = oAuth2Config.CustomURLMapping.AuthURL
  598. customURLMapping.ProfileURL = oAuth2Config.CustomURLMapping.ProfileURL
  599. customURLMapping.EmailURL = oAuth2Config.CustomURLMapping.EmailURL
  600. }
  601. if c.IsSet("use-custom-urls") && c.IsSet("custom-token-url") {
  602. customURLMapping.TokenURL = c.String("custom-token-url")
  603. }
  604. if c.IsSet("use-custom-urls") && c.IsSet("custom-auth-url") {
  605. customURLMapping.AuthURL = c.String("custom-auth-url")
  606. }
  607. if c.IsSet("use-custom-urls") && c.IsSet("custom-profile-url") {
  608. customURLMapping.ProfileURL = c.String("custom-profile-url")
  609. }
  610. if c.IsSet("use-custom-urls") && c.IsSet("custom-email-url") {
  611. customURLMapping.EmailURL = c.String("custom-email-url")
  612. }
  613. oAuth2Config.CustomURLMapping = customURLMapping
  614. source.Cfg = oAuth2Config
  615. return models.UpdateSource(source)
  616. }
  617. func runListAuth(c *cli.Context) error {
  618. if err := initDB(); err != nil {
  619. return err
  620. }
  621. loginSources, err := models.LoginSources()
  622. if err != nil {
  623. return err
  624. }
  625. flags := tabwriter.AlignRight
  626. if c.Bool("vertical-bars") {
  627. flags |= tabwriter.Debug
  628. }
  629. padChar := byte('\t')
  630. if len(c.String("pad-char")) > 0 {
  631. padChar = c.String("pad-char")[0]
  632. }
  633. // loop through each source and print
  634. w := tabwriter.NewWriter(os.Stdout, c.Int("min-width"), c.Int("tab-width"), c.Int("padding"), padChar, flags)
  635. fmt.Fprintf(w, "ID\tName\tType\tEnabled\n")
  636. for _, source := range loginSources {
  637. fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, models.LoginNames[source.Type], source.IsActived)
  638. }
  639. w.Flush()
  640. return nil
  641. }
  642. func runDeleteAuth(c *cli.Context) error {
  643. if !c.IsSet("id") {
  644. return fmt.Errorf("--id flag is missing")
  645. }
  646. if err := initDB(); err != nil {
  647. return err
  648. }
  649. source, err := models.GetLoginSourceByID(c.Int64("id"))
  650. if err != nil {
  651. return err
  652. }
  653. return models.DeleteSource(source)
  654. }