Skip to content

Commit ae5d11f

Browse files
committed
Optimize hints in the output of info and inspect commands
1 parent 78b0c57 commit ae5d11f

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

internal/cli/commands/info.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
"os"
89
"path/filepath"
910
"sort"
1011

@@ -105,6 +106,7 @@ func NewInfoCmd() *cobra.Command {
105106
func ShowInfo(target string, packagesDir string, languageCode string, jsonOutput bool, out io.Writer) error {
106107
reference, err := packageinfo.ParsePackageReference(target)
107108
if err != nil {
109+
writeInfoInspectSuggestion(jsonOutput, out, target)
108110
return writeInfoError(jsonOutput, out, "SCHEMA_ERROR", err.Error(), nil, err)
109111
}
110112
if reference.PackageID == "" {
@@ -815,3 +817,14 @@ func writeInfoError(jsonOutput bool, out io.Writer, code string, message string,
815817
}
816818
return err
817819
}
820+
821+
func writeInfoInspectSuggestion(jsonOutput bool, out io.Writer, target string) {
822+
if jsonOutput {
823+
return
824+
}
825+
info, err := os.Stat(target)
826+
if err != nil || info.IsDir() {
827+
return
828+
}
829+
fmt.Fprintf(out, "Suggestion: If you want to view information about a package file, use `dspm inspect %s` instead.\n", target)
830+
}

internal/cli/commands/info_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,52 @@ func TestShowInfoTextForSingerFiltersModulesAndOmitsStatuses(t *testing.T) {
100100
}
101101
}
102102

103+
func TestShowInfoSuggestsInspectForPackageFile(t *testing.T) {
104+
packageFile := filepath.Join(t.TempDir(), "sample package.dspk")
105+
if err := os.WriteFile(packageFile, []byte("not used"), 0o644); err != nil {
106+
t.Fatalf("write package file: %v", err)
107+
}
108+
109+
var output bytes.Buffer
110+
err := ShowInfo(packageFile, t.TempDir(), "en", false, &output)
111+
if err == nil {
112+
t.Fatalf("ShowInfo() error = nil")
113+
}
114+
115+
got := output.String()
116+
if !strings.Contains(got, "Suggestion:") ||
117+
!strings.Contains(got, "dspm inspect "+packageFile) {
118+
t.Fatalf("output = %q", got)
119+
}
120+
}
121+
122+
func TestShowInfoJSONDoesNotSuggestInspectForPackageFile(t *testing.T) {
123+
packageFile := filepath.Join(t.TempDir(), "sample package.dspk")
124+
if err := os.WriteFile(packageFile, []byte("not used"), 0o644); err != nil {
125+
t.Fatalf("write package file: %v", err)
126+
}
127+
128+
var output bytes.Buffer
129+
err := ShowInfo(packageFile, t.TempDir(), "en", true, &output)
130+
if err == nil {
131+
t.Fatalf("ShowInfo() error = nil")
132+
}
133+
134+
got := output.String()
135+
if strings.Contains(got, "Suggestion:") ||
136+
strings.Contains(got, "dspm inspect") {
137+
t.Fatalf("json output should not include suggestion: %q", got)
138+
}
139+
140+
var payload infoOutput
141+
if err := json.Unmarshal(output.Bytes(), &payload); err != nil {
142+
t.Fatalf("unmarshal output: %v\n%s", err, output.String())
143+
}
144+
if payload.OK || payload.Error == nil {
145+
t.Fatalf("payload = %#v", payload)
146+
}
147+
}
148+
103149
func TestShowInfoJSONReportsAmbiguousVersion(t *testing.T) {
104150
packagesDir := makeInfoTestDatabase(t)
105151

internal/cli/commands/inspect.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,13 @@ func NewInspectCmd() *cobra.Command {
151151
func InspectPackageFile(packageFilePath string, packagesDir string, languageCode string, jsonOutput bool, includeHash bool, out io.Writer) error {
152152
absolutePackageFilePath, err := filepath.Abs(packageFilePath)
153153
if err != nil {
154+
writeInspectInfoSuggestion(jsonOutput, out, packageFilePath)
154155
return writeInspectError(jsonOutput, out, "IO_ERROR", fmt.Sprintf("resolve package file path: %v", err), err)
155156
}
156157

157158
reader, err := openPackageFileReader(absolutePackageFilePath)
158159
if err != nil {
160+
writeInspectInfoSuggestion(jsonOutput, out, packageFilePath)
159161
return writeInspectError(jsonOutput, out, "IO_ERROR", err.Error(), err)
160162
}
161163
defer reader.Close()
@@ -558,3 +560,14 @@ func writeInspectError(jsonOutput bool, out io.Writer, code string, message stri
558560
}
559561
return err
560562
}
563+
564+
func writeInspectInfoSuggestion(jsonOutput bool, out io.Writer, packageFilePath string) {
565+
if jsonOutput {
566+
return
567+
}
568+
reference, err := packageinfo.ParsePackageReference(packageFilePath)
569+
if err != nil || reference.PackageID == "" {
570+
return
571+
}
572+
fmt.Fprintf(out, "Suggestion: If you want to view information about an installed package, use `dspm info %s` instead.\n", reference.String())
573+
}

internal/cli/commands/inspect_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,42 @@ func TestInspectPackageFileJSONReportsInstalledStatus(t *testing.T) {
131131
}
132132
}
133133

134+
func TestInspectPackageFileSuggestsInfoForPackageReference(t *testing.T) {
135+
var output bytes.Buffer
136+
err := InspectPackageFile("vendor/package@1.0.0.0", t.TempDir(), "en", false, false, &output)
137+
if err == nil {
138+
t.Fatalf("InspectPackageFile() error = nil")
139+
}
140+
141+
got := output.String()
142+
if !strings.Contains(got, "Suggestion:") ||
143+
!strings.Contains(got, "dspm info vendor/package@1.0.0.0") {
144+
t.Fatalf("output = %q", got)
145+
}
146+
}
147+
148+
func TestInspectPackageFileJSONDoesNotSuggestInfoForPackageReference(t *testing.T) {
149+
var output bytes.Buffer
150+
err := InspectPackageFile("vendor/package@1.0.0.0", t.TempDir(), "en", true, false, &output)
151+
if err == nil {
152+
t.Fatalf("InspectPackageFile() error = nil")
153+
}
154+
155+
got := output.String()
156+
if strings.Contains(got, "Suggestion:") ||
157+
strings.Contains(got, "dspm info") {
158+
t.Fatalf("json output should not include suggestion: %q", got)
159+
}
160+
161+
var payload inspectOutput
162+
if err := json.Unmarshal(output.Bytes(), &payload); err != nil {
163+
t.Fatalf("unmarshal output: %v\n%s", err, output.String())
164+
}
165+
if payload.OK || payload.Error == nil {
166+
t.Fatalf("payload = %#v", payload)
167+
}
168+
}
169+
134170
func TestInspectPackageFileTextIncludesInstalledReferenceNames(t *testing.T) {
135171
packagesDir := t.TempDir()
136172
db, err := packagedatabase.Open(filepath.Join(packagesDir, "packages.db"))

0 commit comments

Comments
 (0)