| 53 | } |
| 54 | |
| 55 | func (a *App) ViewCodespace(ctx context.Context, opts *viewOptions) error { |
| 56 | // If we are in a codespace and a codespace name wasn't provided, show the details for the codespace we are connected to |
| 57 | if (os.Getenv("CODESPACES") == "true") && opts.selector.codespaceName == "" { |
| 58 | codespaceName := os.Getenv("CODESPACE_NAME") |
| 59 | opts.selector.codespaceName = codespaceName |
| 60 | } |
| 61 | |
| 62 | selectedCodespace, err := opts.selector.Select(ctx) |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | |
| 67 | if err := a.io.StartPager(); err != nil { |
| 68 | a.errLogger.Printf("error starting pager: %v", err) |
| 69 | } |
| 70 | defer a.io.StopPager() |
| 71 | |
| 72 | if opts.exporter != nil { |
| 73 | return opts.exporter.Write(a.io, selectedCodespace) |
| 74 | } |
| 75 | |
| 76 | //nolint:staticcheck // SA1019: Showing NAME|VALUE headers adds nothing to table. |
| 77 | tp := tableprinter.New(a.io, tableprinter.NoHeader) |
| 78 | c := codespace{selectedCodespace} |
| 79 | formattedName := formatNameForVSCSTarget(c.Name, c.VSCSTarget) |
| 80 | |
| 81 | // Create an array of fields to display in the table with their values |
| 82 | fields := []struct { |
| 83 | name string |
| 84 | value string |
| 85 | }{ |
| 86 | {"Name", formattedName}, |
| 87 | {"State", c.State}, |
| 88 | {"Repository", c.Repository.FullName}, |
| 89 | {"Git Status", formatGitStatus(c)}, |
| 90 | {"Devcontainer Path", c.DevContainerPath}, |
| 91 | {"Machine Display Name", c.Machine.DisplayName}, |
| 92 | {"Idle Timeout", fmt.Sprintf("%d minutes", c.IdleTimeoutMinutes)}, |
| 93 | {"Created At", c.CreatedAt}, |
| 94 | {"Retention Period", formatRetentionPeriodDays(c)}, |
| 95 | } |
| 96 | |
| 97 | for _, field := range fields { |
| 98 | // Don't display the field if it is empty and we are printing to a TTY |
| 99 | if !a.io.IsStdoutTTY() || field.value != "" { |
| 100 | tp.AddField(field.name) |
| 101 | tp.AddField(field.value) |
| 102 | tp.EndRow() |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | err = tp.Render() |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | |
| 111 | return nil |
| 112 | } |