Run executes the command.
(ctx context.Context, args []string)
| 17 | |
| 18 | // Run executes the command. |
| 19 | func (c *DialMembersCommand) Run(ctx context.Context, args []string) error { |
| 20 | // Create a flag set to read the config path & read the dial ID. |
| 21 | fs := flag.NewFlagSet("wtf-dial-members", flag.ContinueOnError) |
| 22 | attachConfigFlags(fs, &c.ConfigPath) |
| 23 | if err := fs.Parse(args); err != nil { |
| 24 | return err |
| 25 | } else if fs.NArg() == 0 { |
| 26 | return fmt.Errorf("Dial ID required.") |
| 27 | } else if fs.NArg() > 1 { |
| 28 | return fmt.Errorf("Only one dial ID allowed.") |
| 29 | } |
| 30 | |
| 31 | // Parse dial ID from first arg. |
| 32 | id, err := strconv.Atoi(fs.Arg(0)) |
| 33 | if err != nil { |
| 34 | return fmt.Errorf("Invalid dial ID.") |
| 35 | } |
| 36 | |
| 37 | // Load configuration file. |
| 38 | config, err := ReadConfigFile(c.ConfigPath) |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | |
| 43 | // Authenticate user with API key. |
| 44 | ctx = wtf.NewContextWithUser(ctx, &wtf.User{APIKey: config.APIKey}) |
| 45 | |
| 46 | // Instantiate HTTP user service and fetch dial. |
| 47 | // Members are automatically attached to the dial. |
| 48 | dialService := http.NewDialService(http.NewClient(config.URL)) |
| 49 | dial, err := dialService.FindDialByID(ctx, id) |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | // Iterate over membrships and print the name & value. |
| 55 | for _, membership := range dial.Memberships { |
| 56 | fmt.Printf( |
| 57 | "%s\t%d\n", |
| 58 | membership.User.Name, |
| 59 | membership.Value, |
| 60 | ) |
| 61 | } |
| 62 | |
| 63 | return nil |
| 64 | } |
| 65 | |
| 66 | // usage prints command usage information to STDOUT. |
| 67 | func (c *DialMembersCommand) usage() { |
nothing calls this directly
no test coverage detected