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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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 err = user.SetPassword(c.String("password")); err != nil {
  329. return err
  330. }
  331. if err = models.UpdateUserCols(user, "passwd", "passwd_hash_algo", "salt"); err != nil {
  332. return err
  333. }
  334. fmt.Printf("%s's password has been successfully updated!\n", user.Name)
  335. return nil
  336. }
  337. func runCreateUser(c *cli.Context) error {
  338. if err := argsSet(c, "email"); err != nil {
  339. return err
  340. }
  341. if c.IsSet("name") && c.IsSet("username") {
  342. return errors.New("Cannot set both --name and --username flags")
  343. }
  344. if !c.IsSet("name") && !c.IsSet("username") {
  345. return errors.New("One of --name or --username flags must be set")
  346. }
  347. if c.IsSet("password") && c.IsSet("random-password") {
  348. return errors.New("cannot set both -random-password and -password flags")
  349. }
  350. var username string
  351. if c.IsSet("username") {
  352. username = c.String("username")
  353. } else {
  354. username = c.String("name")
  355. fmt.Fprintf(os.Stderr, "--name flag is deprecated. Use --username instead.\n")
  356. }
  357. if err := initDB(); err != nil {
  358. return err
  359. }
  360. var password string
  361. if c.IsSet("password") {
  362. password = c.String("password")
  363. } else if c.IsSet("random-password") {
  364. var err error
  365. password, err = pwd.Generate(c.Int("random-password-length"))
  366. if err != nil {
  367. return err
  368. }
  369. fmt.Printf("generated random password is '%s'\n", password)
  370. } else {
  371. return errors.New("must set either password or random-password flag")
  372. }
  373. // always default to true
  374. var changePassword = true
  375. // If this is the first user being created.
  376. // Take it as the admin and don't force a password update.
  377. if n := models.CountUsers(); n == 0 {
  378. changePassword = false
  379. }
  380. if c.IsSet("must-change-password") {
  381. changePassword = c.Bool("must-change-password")
  382. }
  383. u := &models.User{
  384. Name: username,
  385. Email: c.String("email"),
  386. Passwd: password,
  387. IsActive: true,
  388. IsAdmin: c.Bool("admin"),
  389. MustChangePassword: changePassword,
  390. Theme: setting.UI.DefaultTheme,
  391. }
  392. if err := models.CreateUser(u); err != nil {
  393. return fmt.Errorf("CreateUser: %v", err)
  394. }
  395. if c.Bool("access-token") {
  396. t := &models.AccessToken{
  397. Name: "gitea-admin",
  398. UID: u.ID,
  399. }
  400. if err := models.NewAccessToken(t); err != nil {
  401. return err
  402. }
  403. fmt.Printf("Access token was successfully created... %s\n", t.Token)
  404. }
  405. fmt.Printf("New user '%s' has been successfully created!\n", username)
  406. return nil
  407. }
  408. func runListUsers(c *cli.Context) error {
  409. if err := initDB(); err != nil {
  410. return err
  411. }
  412. users, err := models.GetAllUsers()
  413. if err != nil {
  414. return err
  415. }
  416. w := tabwriter.NewWriter(os.Stdout, 5, 0, 1, ' ', 0)
  417. if c.IsSet("admin") {
  418. fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\n")
  419. for _, u := range users {
  420. if u.IsAdmin {
  421. fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", u.ID, u.Name, u.Email, u.IsActive)
  422. }
  423. }
  424. } else {
  425. fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\tIsAdmin\n")
  426. for _, u := range users {
  427. fmt.Fprintf(w, "%d\t%s\t%s\t%t\t%t\n", u.ID, u.Name, u.Email, u.IsActive, u.IsAdmin)
  428. }
  429. }
  430. w.Flush()
  431. return nil
  432. }
  433. func runDeleteUser(c *cli.Context) error {
  434. if !c.IsSet("id") && !c.IsSet("username") && !c.IsSet("email") {
  435. return fmt.Errorf("You must provide the id, username or email of a user to delete")
  436. }
  437. if err := initDB(); err != nil {
  438. return err
  439. }
  440. var err error
  441. var user *models.User
  442. if c.IsSet("email") {
  443. user, err = models.GetUserByEmail(c.String("email"))
  444. } else if c.IsSet("username") {
  445. user, err = models.GetUserByName(c.String("username"))
  446. } else {
  447. user, err = models.GetUserByID(c.Int64("id"))
  448. }
  449. if err != nil {
  450. return err
  451. }
  452. if c.IsSet("username") && user.LowerName != strings.ToLower(strings.TrimSpace(c.String("username"))) {
  453. 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"))
  454. }
  455. if c.IsSet("id") && user.ID != c.Int64("id") {
  456. return fmt.Errorf("The user %s does not match the provided id %d", user.Name, c.Int64("id"))
  457. }
  458. return models.DeleteUser(user)
  459. }
  460. func runRepoSyncReleases(_ *cli.Context) error {
  461. if err := initDB(); err != nil {
  462. return err
  463. }
  464. log.Trace("Synchronizing repository releases (this may take a while)")
  465. for page := 1; ; page++ {
  466. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  467. ListOptions: models.ListOptions{
  468. PageSize: models.RepositoryListDefaultPageSize,
  469. Page: page,
  470. },
  471. Private: true,
  472. })
  473. if err != nil {
  474. return fmt.Errorf("SearchRepositoryByName: %v", err)
  475. }
  476. if len(repos) == 0 {
  477. break
  478. }
  479. log.Trace("Processing next %d repos of %d", len(repos), count)
  480. for _, repo := range repos {
  481. log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath())
  482. gitRepo, err := git.OpenRepository(repo.RepoPath())
  483. if err != nil {
  484. log.Warn("OpenRepository: %v", err)
  485. continue
  486. }
  487. oldnum, err := getReleaseCount(repo.ID)
  488. if err != nil {
  489. log.Warn(" GetReleaseCountByRepoID: %v", err)
  490. }
  491. log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)
  492. if err = repo_module.SyncReleasesWithTags(repo, gitRepo); err != nil {
  493. log.Warn(" SyncReleasesWithTags: %v", err)
  494. gitRepo.Close()
  495. continue
  496. }
  497. count, err = getReleaseCount(repo.ID)
  498. if err != nil {
  499. log.Warn(" GetReleaseCountByRepoID: %v", err)
  500. gitRepo.Close()
  501. continue
  502. }
  503. log.Trace(" repo %s releases synchronized to tags: from %d to %d",
  504. repo.FullName(), oldnum, count)
  505. gitRepo.Close()
  506. }
  507. }
  508. return nil
  509. }
  510. func getReleaseCount(id int64) (int64, error) {
  511. return models.GetReleaseCountByRepoID(
  512. id,
  513. models.FindReleasesOptions{
  514. IncludeTags: true,
  515. },
  516. )
  517. }
  518. func runRegenerateHooks(_ *cli.Context) error {
  519. if err := initDB(); err != nil {
  520. return err
  521. }
  522. return repo_module.SyncRepositoryHooks(graceful.GetManager().ShutdownContext())
  523. }
  524. func runRegenerateKeys(_ *cli.Context) error {
  525. if err := initDB(); err != nil {
  526. return err
  527. }
  528. return models.RewriteAllPublicKeys()
  529. }
  530. func parseOAuth2Config(c *cli.Context) *models.OAuth2Config {
  531. var customURLMapping *oauth2.CustomURLMapping
  532. if c.IsSet("use-custom-urls") {
  533. customURLMapping = &oauth2.CustomURLMapping{
  534. TokenURL: c.String("custom-token-url"),
  535. AuthURL: c.String("custom-auth-url"),
  536. ProfileURL: c.String("custom-profile-url"),
  537. EmailURL: c.String("custom-email-url"),
  538. }
  539. } else {
  540. customURLMapping = nil
  541. }
  542. return &models.OAuth2Config{
  543. Provider: c.String("provider"),
  544. ClientID: c.String("key"),
  545. ClientSecret: c.String("secret"),
  546. OpenIDConnectAutoDiscoveryURL: c.String("auto-discover-url"),
  547. CustomURLMapping: customURLMapping,
  548. IconURL: c.String("icon-url"),
  549. }
  550. }
  551. func runAddOauth(c *cli.Context) error {
  552. if err := initDB(); err != nil {
  553. return err
  554. }
  555. return models.CreateLoginSource(&models.LoginSource{
  556. Type: models.LoginOAuth2,
  557. Name: c.String("name"),
  558. IsActived: true,
  559. Cfg: parseOAuth2Config(c),
  560. })
  561. }
  562. func runUpdateOauth(c *cli.Context) error {
  563. if !c.IsSet("id") {
  564. return fmt.Errorf("--id flag is missing")
  565. }
  566. if err := initDB(); err != nil {
  567. return err
  568. }
  569. source, err := models.GetLoginSourceByID(c.Int64("id"))
  570. if err != nil {
  571. return err
  572. }
  573. oAuth2Config := source.OAuth2()
  574. if c.IsSet("name") {
  575. source.Name = c.String("name")
  576. }
  577. if c.IsSet("provider") {
  578. oAuth2Config.Provider = c.String("provider")
  579. }
  580. if c.IsSet("key") {
  581. oAuth2Config.ClientID = c.String("key")
  582. }
  583. if c.IsSet("secret") {
  584. oAuth2Config.ClientSecret = c.String("secret")
  585. }
  586. if c.IsSet("auto-discover-url") {
  587. oAuth2Config.OpenIDConnectAutoDiscoveryURL = c.String("auto-discover-url")
  588. }
  589. if c.IsSet("icon-url") {
  590. oAuth2Config.IconURL = c.String("icon-url")
  591. }
  592. // update custom URL mapping
  593. var customURLMapping = &oauth2.CustomURLMapping{}
  594. if oAuth2Config.CustomURLMapping != nil {
  595. customURLMapping.TokenURL = oAuth2Config.CustomURLMapping.TokenURL
  596. customURLMapping.AuthURL = oAuth2Config.CustomURLMapping.AuthURL
  597. customURLMapping.ProfileURL = oAuth2Config.CustomURLMapping.ProfileURL
  598. customURLMapping.EmailURL = oAuth2Config.CustomURLMapping.EmailURL
  599. }
  600. if c.IsSet("use-custom-urls") && c.IsSet("custom-token-url") {
  601. customURLMapping.TokenURL = c.String("custom-token-url")
  602. }
  603. if c.IsSet("use-custom-urls") && c.IsSet("custom-auth-url") {
  604. customURLMapping.AuthURL = c.String("custom-auth-url")
  605. }
  606. if c.IsSet("use-custom-urls") && c.IsSet("custom-profile-url") {
  607. customURLMapping.ProfileURL = c.String("custom-profile-url")
  608. }
  609. if c.IsSet("use-custom-urls") && c.IsSet("custom-email-url") {
  610. customURLMapping.EmailURL = c.String("custom-email-url")
  611. }
  612. oAuth2Config.CustomURLMapping = customURLMapping
  613. source.Cfg = oAuth2Config
  614. return models.UpdateSource(source)
  615. }
  616. func runListAuth(c *cli.Context) error {
  617. if err := initDB(); err != nil {
  618. return err
  619. }
  620. loginSources, err := models.LoginSources()
  621. if err != nil {
  622. return err
  623. }
  624. flags := tabwriter.AlignRight
  625. if c.Bool("vertical-bars") {
  626. flags |= tabwriter.Debug
  627. }
  628. padChar := byte('\t')
  629. if len(c.String("pad-char")) > 0 {
  630. padChar = c.String("pad-char")[0]
  631. }
  632. // loop through each source and print
  633. w := tabwriter.NewWriter(os.Stdout, c.Int("min-width"), c.Int("tab-width"), c.Int("padding"), padChar, flags)
  634. fmt.Fprintf(w, "ID\tName\tType\tEnabled\n")
  635. for _, source := range loginSources {
  636. fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, models.LoginNames[source.Type], source.IsActived)
  637. }
  638. w.Flush()
  639. return nil
  640. }
  641. func runDeleteAuth(c *cli.Context) error {
  642. if !c.IsSet("id") {
  643. return fmt.Errorf("--id flag is missing")
  644. }
  645. if err := initDB(); err != nil {
  646. return err
  647. }
  648. source, err := models.GetLoginSourceByID(c.Int64("id"))
  649. if err != nil {
  650. return err
  651. }
  652. return models.DeleteSource(source)
  653. }