|
| 1 | +package infocmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log/slog" |
| 10 | + "os" |
| 11 | + "os/exec" |
| 12 | + "runtime" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/a-h/templ" |
| 16 | + "github.com/a-h/templ/cmd/templ/lspcmd/pls" |
| 17 | +) |
| 18 | + |
| 19 | +type Arguments struct { |
| 20 | + JSON bool `flag:"json" help:"Output info as JSON."` |
| 21 | +} |
| 22 | + |
| 23 | +type Info struct { |
| 24 | + OS struct { |
| 25 | + GOOS string `json:"goos"` |
| 26 | + GOARCH string `json:"goarch"` |
| 27 | + } `json:"os"` |
| 28 | + Go ToolInfo `json:"go"` |
| 29 | + Gopls ToolInfo `json:"gopls"` |
| 30 | + Templ ToolInfo `json:"templ"` |
| 31 | +} |
| 32 | + |
| 33 | +type ToolInfo struct { |
| 34 | + Location string `json:"location"` |
| 35 | + Version string `json:"version"` |
| 36 | + OK bool `json:"ok"` |
| 37 | + Message string `json:"message,omitempty"` |
| 38 | +} |
| 39 | + |
| 40 | +func getGoInfo() (d ToolInfo) { |
| 41 | + // Find Go. |
| 42 | + var err error |
| 43 | + d.Location, err = exec.LookPath("go") |
| 44 | + if err != nil { |
| 45 | + d.Message = fmt.Sprintf("failed to find go: %v", err) |
| 46 | + return |
| 47 | + } |
| 48 | + // Run go to find the version. |
| 49 | + cmd := exec.Command(d.Location, "version") |
| 50 | + v, err := cmd.Output() |
| 51 | + if err != nil { |
| 52 | + d.Message = fmt.Sprintf("failed to get go version, check that Go is installed: %v", err) |
| 53 | + return |
| 54 | + } |
| 55 | + d.Version = strings.TrimSpace(string(v)) |
| 56 | + d.OK = true |
| 57 | + return |
| 58 | +} |
| 59 | + |
| 60 | +func getGoplsInfo() (d ToolInfo) { |
| 61 | + var err error |
| 62 | + d.Location, err = pls.FindGopls() |
| 63 | + if err != nil { |
| 64 | + d.Message = fmt.Sprintf("failed to find gopls: %v", err) |
| 65 | + return |
| 66 | + } |
| 67 | + cmd := exec.Command(d.Location, "version") |
| 68 | + v, err := cmd.Output() |
| 69 | + if err != nil { |
| 70 | + d.Message = fmt.Sprintf("failed to get gopls version: %v", err) |
| 71 | + return |
| 72 | + } |
| 73 | + d.Version = strings.TrimSpace(string(v)) |
| 74 | + d.OK = true |
| 75 | + return |
| 76 | +} |
| 77 | + |
| 78 | +func getTemplInfo() (d ToolInfo) { |
| 79 | + // Find templ. |
| 80 | + var err error |
| 81 | + d.Location, err = findTempl() |
| 82 | + if err != nil { |
| 83 | + d.Message = err.Error() |
| 84 | + return |
| 85 | + } |
| 86 | + // Run templ to find the version. |
| 87 | + cmd := exec.Command(d.Location, "version") |
| 88 | + v, err := cmd.Output() |
| 89 | + if err != nil { |
| 90 | + d.Message = fmt.Sprintf("failed to get templ version: %v", err) |
| 91 | + return |
| 92 | + } |
| 93 | + d.Version = strings.TrimSpace(string(v)) |
| 94 | + if d.Version != templ.Version() { |
| 95 | + d.Message = fmt.Sprintf("version mismatch - you're running %q at the command line, but the version in the path is %q", templ.Version(), d.Version) |
| 96 | + return |
| 97 | + } |
| 98 | + d.OK = true |
| 99 | + return |
| 100 | +} |
| 101 | + |
| 102 | +func findTempl() (location string, err error) { |
| 103 | + executableName := "templ" |
| 104 | + if runtime.GOOS == "windows" { |
| 105 | + executableName = "templ.exe" |
| 106 | + } |
| 107 | + executableName, err = exec.LookPath(executableName) |
| 108 | + if err == nil { |
| 109 | + // Found on the path. |
| 110 | + return executableName, nil |
| 111 | + } |
| 112 | + |
| 113 | + // Unexpected error. |
| 114 | + if !errors.Is(err, exec.ErrNotFound) { |
| 115 | + return "", fmt.Errorf("unexpected error looking for templ: %w", err) |
| 116 | + } |
| 117 | + |
| 118 | + return "", fmt.Errorf("templ is not in the path (%q). You can install templ with `go install github.com/a-h/templ/cmd/templ@latest`", os.Getenv("PATH")) |
| 119 | +} |
| 120 | + |
| 121 | +func getInfo() (d Info) { |
| 122 | + d.OS.GOOS = runtime.GOOS |
| 123 | + d.OS.GOARCH = runtime.GOARCH |
| 124 | + d.Go = getGoInfo() |
| 125 | + d.Gopls = getGoplsInfo() |
| 126 | + d.Templ = getTemplInfo() |
| 127 | + return |
| 128 | +} |
| 129 | + |
| 130 | +func Run(ctx context.Context, log *slog.Logger, stdout io.Writer, args Arguments) (err error) { |
| 131 | + info := getInfo() |
| 132 | + if args.JSON { |
| 133 | + enc := json.NewEncoder(stdout) |
| 134 | + enc.SetIndent("", " ") |
| 135 | + return enc.Encode(info) |
| 136 | + } |
| 137 | + log.Info("os", slog.String("goos", info.OS.GOOS), slog.String("goarch", info.OS.GOARCH)) |
| 138 | + logInfo(ctx, log, "go", info.Go) |
| 139 | + logInfo(ctx, log, "gopls", info.Gopls) |
| 140 | + logInfo(ctx, log, "templ", info.Templ) |
| 141 | + return nil |
| 142 | +} |
| 143 | + |
| 144 | +func logInfo(ctx context.Context, log *slog.Logger, name string, ti ToolInfo) { |
| 145 | + level := slog.LevelInfo |
| 146 | + if !ti.OK { |
| 147 | + level = slog.LevelError |
| 148 | + } |
| 149 | + args := []any{ |
| 150 | + slog.String("location", ti.Location), |
| 151 | + slog.String("version", ti.Version), |
| 152 | + } |
| 153 | + if ti.Message != "" { |
| 154 | + args = append(args, slog.String("message", ti.Message)) |
| 155 | + } |
| 156 | + log.Log(ctx, level, name, args...) |
| 157 | +} |
0 commit comments