-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from hanabi-rest/feat/private-token
Update CLI tool usage and add config command
- Loading branch information
Showing
8 changed files
with
180 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@hanabi.rest/cli": minor | ||
--- | ||
|
||
Add token access for private applications |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import xdgAppPaths from "xdg-app-paths"; | ||
import path from "node:path"; | ||
import { mkdirSync, chmodSync, writeFileSync, readFileSync } from "node:fs"; | ||
import TOML from "@iarna/toml"; | ||
|
||
export const USER_AUTH_CONFIG_FILE = "config.toml"; | ||
|
||
type AuthConfig = { | ||
api_token: string; | ||
}; | ||
|
||
export function getGlobalConfigPath() { | ||
|
||
return xdgAppPaths(".hanabi").config(); | ||
} | ||
|
||
export function writeAuthConfigFile(config: AuthConfig) { | ||
const authConfigFilePath = path.join( | ||
getGlobalConfigPath(), | ||
USER_AUTH_CONFIG_FILE | ||
); | ||
mkdirSync(path.dirname(authConfigFilePath), { | ||
recursive: true, | ||
}); | ||
writeFileSync( | ||
path.join(authConfigFilePath), | ||
TOML.stringify(config as TOML.JsonMap), | ||
{ encoding: "utf-8" } | ||
); | ||
|
||
chmodSync(path.join(authConfigFilePath), 0o600); | ||
|
||
} | ||
export function readConfig() { | ||
const authConfigFilePath = path.join( | ||
getGlobalConfigPath(), | ||
USER_AUTH_CONFIG_FILE | ||
); | ||
try { | ||
const normalizedInput = readFileSync(authConfigFilePath, { encoding: "utf-8" }).replace(/\r\n/g, "\n"); | ||
|
||
return TOML.parse(normalizedInput) as AuthConfig; | ||
} catch (e) { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters