Skip to content

Commit 181f579

Browse files
feat: add a helper for requiring authentication to use actions (#11)
1 parent 22b03c5 commit 181f579

File tree

5 files changed

+98
-38
lines changed

5 files changed

+98
-38
lines changed

deno.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/act.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { env } from "./env.ts";
22
import * as credentials from "./credentials.ts";
33
import * as config from "./config.ts";
4-
import { strip } from "./ansi.ts";
4+
import { info, strip } from "./ansi.ts";
55

66
/**
77
* Logs a message to the console in the appropriate format based on the
@@ -74,6 +74,51 @@ export function act<
7474
};
7575
}
7676

77+
/**
78+
* A higher order function that needs to be added to every action that requires
79+
* the user to be logged in. This will also print the return value of an action
80+
* function in the appropriate format based on the CLI options.
81+
*
82+
* @param fn - An action function that returns a `PrintsFormats` object.
83+
* @example
84+
* ```ts
85+
* cli.action(act.ifLoggedIn(async () => {
86+
* return { value: "Hello World" }
87+
* }))
88+
* ```
89+
*/
90+
act.ifLoggedIn = <
91+
Fn extends (...args: any[]) => Formats | Promise<Formats>,
92+
>(fn: Fn) => {
93+
return async function action(...args: Parameters<Fn>): Promise<void> {
94+
const opt = args[0];
95+
const isLoggedIn = !!(opt.apiKey || await config.get("team"));
96+
const availableTeams = await credentials.list();
97+
const hasTeams = availableTeams.length > 0;
98+
const loginHelper = !hasTeams
99+
? `Run "${info("pspace login")}" to log in.`
100+
: `Run "${
101+
info(`pspace config set team`)
102+
}" to choose a team. \n\nAvailable teams: \n › ${
103+
availableTeams.join("\n › ")
104+
}`;
105+
106+
const selectMessage = `You must ${
107+
hasTeams ? "select a team" : "be logged in"
108+
} to run this command.`;
109+
110+
if (!isLoggedIn) {
111+
throw new config.ConfigError(
112+
`${selectMessage}
113+
114+
${loginHelper}`,
115+
);
116+
}
117+
118+
return act(fn)(...args);
119+
};
120+
};
121+
77122
type Formats =
78123
| {
79124
/**

lib/cli.ts

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,30 @@ cli
103103
console.log("init");
104104
})
105105
.command("create")
106-
.action(() => {
106+
.action(act.ifLoggedIn(() => {
107107
console.log("create");
108-
})
108+
return { value: "" };
109+
}))
109110
.command("update")
110-
.action(() => {
111+
.action(act.ifLoggedIn(() => {
111112
console.log("update");
112-
})
113+
return { value: "" };
114+
}))
113115
.command("delete")
114-
.action(() => {
116+
.action(act.ifLoggedIn(() => {
115117
console.log("delete");
116-
})
118+
return { value: "" };
119+
}))
117120
.command("list")
118-
.action(() => {
121+
.action(act.ifLoggedIn(() => {
119122
console.log("list");
120-
})
123+
return { value: "" };
124+
}))
121125
.command("get")
122-
.action(() => {
123-
console.log("list");
124-
}),
126+
.action(act.ifLoggedIn(() => {
127+
console.log("get");
128+
return { value: "" };
129+
})),
125130
)
126131
.action(function () {
127132
this.showHelp();
@@ -139,25 +144,30 @@ cli
139144
console.log("init");
140145
})
141146
.command("create")
142-
.action(() => {
147+
.action(act.ifLoggedIn(() => {
143148
console.log("create");
144-
})
149+
return { value: "" };
150+
}))
145151
.command("update")
146-
.action(() => {
152+
.action(act.ifLoggedIn(() => {
147153
console.log("update");
148-
})
154+
return { value: "" };
155+
}))
149156
.command("delete")
150-
.action(() => {
157+
.action(act.ifLoggedIn(() => {
151158
console.log("delete");
152-
})
159+
return { value: "" };
160+
}))
153161
.command("list")
154-
.action(() => {
162+
.action(act.ifLoggedIn(() => {
155163
console.log("list");
156-
})
164+
return { value: "" };
165+
}))
157166
.command("get")
158-
.action(() => {
167+
.action(act.ifLoggedIn(() => {
159168
console.log("get");
160-
}),
169+
return { value: "" };
170+
})),
161171
)
162172
.action(function () {
163173
this.showHelp();
@@ -175,25 +185,30 @@ cli
175185
console.log("init");
176186
})
177187
.command("create")
178-
.action(() => {
188+
.action(act.ifLoggedIn(() => {
179189
console.log("create");
180-
})
190+
return { value: "" };
191+
}))
181192
.command("update")
182-
.action(() => {
193+
.action(act.ifLoggedIn(() => {
183194
console.log("update");
184-
})
195+
return { value: "" };
196+
}))
185197
.command("delete")
186-
.action(() => {
198+
.action(act.ifLoggedIn(() => {
187199
console.log("delete");
188-
})
200+
return { value: "" };
201+
}))
189202
.command("list")
190-
.action(() => {
203+
.action(act.ifLoggedIn(() => {
191204
console.log("list");
192-
})
205+
return { value: "" };
206+
}))
193207
.command("get")
194-
.action(() => {
195-
console.log("list");
196-
}),
208+
.action(act.ifLoggedIn(() => {
209+
console.log("get");
210+
return { value: "" };
211+
})),
197212
)
198213
.action(function () {
199214
this.showHelp();

lib/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export const schema = z.object({
124124

125125
const teams = await credentials.list();
126126

127-
logger.critical(
127+
logger.error(
128128
`Team "${value}" was not found in your credentials file. Retaining current team.`,
129129
);
130130

lib/paperspace-graphql.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4169,7 +4169,7 @@ export type ViewerQuery = {
41694169
__typename?: "Query";
41704170
viewer?: {
41714171
__typename?: "Viewer";
4172-
team: { __typename?: "Team"; namespace: string; handle: string };
4172+
team: { __typename?: "Team"; namespace: string };
41734173
} | null;
41744174
};
41754175

@@ -5841,9 +5841,6 @@ export const ViewerDocument = {
58415841
"selections": [{
58425842
"kind": "Field",
58435843
"name": { "kind": "Name", "value": "namespace" },
5844-
}, {
5845-
"kind": "Field",
5846-
"name": { "kind": "Name", "value": "handle" },
58475844
}],
58485845
},
58495846
}],

0 commit comments

Comments
 (0)