hetzner-robot-cli 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/config.js ADDED
@@ -0,0 +1,109 @@
1
+ import { config } from 'dotenv';
2
+ import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs';
3
+ import { homedir } from 'os';
4
+ import { join } from 'path';
5
+ import { input, password as passwordPrompt, confirm } from '@inquirer/prompts';
6
+ config();
7
+ const CONFIG_DIR = join(homedir(), '.hetzner-cli');
8
+ const CONFIG_FILE = join(CONFIG_DIR, 'config.json');
9
+ /**
10
+ * Ensure config directory exists
11
+ */
12
+ function ensureConfigDir() {
13
+ if (!existsSync(CONFIG_DIR)) {
14
+ mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
15
+ }
16
+ }
17
+ /**
18
+ * Load configuration from file
19
+ */
20
+ export function loadConfig() {
21
+ if (!existsSync(CONFIG_FILE)) {
22
+ return {};
23
+ }
24
+ try {
25
+ const data = readFileSync(CONFIG_FILE, 'utf-8');
26
+ return JSON.parse(data);
27
+ }
28
+ catch {
29
+ return {};
30
+ }
31
+ }
32
+ /**
33
+ * Save configuration to file
34
+ */
35
+ export function saveConfig(cfg) {
36
+ ensureConfigDir();
37
+ writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2), { mode: 0o600 });
38
+ }
39
+ /**
40
+ * Clear saved configuration
41
+ */
42
+ export function clearConfig() {
43
+ if (existsSync(CONFIG_FILE)) {
44
+ writeFileSync(CONFIG_FILE, '{}', { mode: 0o600 });
45
+ }
46
+ }
47
+ /**
48
+ * Get credentials from environment variables, config file, or prompt
49
+ */
50
+ export function getCredentials() {
51
+ // First try environment variables
52
+ const envUser = process.env.HETZNER_ROBOT_USER;
53
+ const envPassword = process.env.HETZNER_ROBOT_PASSWORD;
54
+ if (envUser && envPassword) {
55
+ return { user: envUser, password: envPassword };
56
+ }
57
+ // Then try config file
58
+ const cfg = loadConfig();
59
+ if (cfg.user && cfg.password) {
60
+ return { user: cfg.user, password: cfg.password };
61
+ }
62
+ return null;
63
+ }
64
+ /**
65
+ * Check if credentials are configured
66
+ */
67
+ export function hasCredentials() {
68
+ return getCredentials() !== null;
69
+ }
70
+ /**
71
+ * Interactive login prompt
72
+ */
73
+ export async function promptLogin() {
74
+ console.log('');
75
+ console.log('Hetzner Robot API Authentication');
76
+ console.log('─'.repeat(40));
77
+ console.log('');
78
+ console.log('To get your API credentials:');
79
+ console.log('1. Go to https://robot.hetzner.com');
80
+ console.log('2. Navigate to: Settings > Web service settings');
81
+ console.log('3. Create a new web service user');
82
+ console.log('');
83
+ console.log('Note: This is separate from your main Hetzner login.');
84
+ console.log('');
85
+ const user = await input({
86
+ message: 'Web service username:',
87
+ validate: (v) => v.length > 0 || 'Username is required',
88
+ });
89
+ const password = await passwordPrompt({
90
+ message: 'Web service password:',
91
+ validate: (v) => v.length > 0 || 'Password is required',
92
+ });
93
+ const save = await confirm({
94
+ message: 'Save credentials to ~/.hetzner-cli/config.json?',
95
+ default: true,
96
+ });
97
+ if (save) {
98
+ saveConfig({ user, password });
99
+ console.log('');
100
+ console.log('Credentials saved.');
101
+ }
102
+ return { user, password };
103
+ }
104
+ /**
105
+ * Get credentials, prompting if necessary
106
+ */
107
+ export async function requireCredentials() {
108
+ return getCredentials() ?? promptLogin();
109
+ }
@@ -0,0 +1,85 @@
1
+ import type { Server, ServerDetails, Reset, IP, Subnet, Failover, Rdns, SshKey, Firewall, FirewallTemplate, VSwitch, StorageBox, StorageBoxSnapshot, StorageBoxSubaccount, Traffic, Wol, ServerProduct, ServerMarketProduct, ServerTransaction, RescueConfig, LinuxConfig, BootConfig, Cancellation } from './types.js';
2
+ export declare const colors: {
3
+ reset: string;
4
+ bold: string;
5
+ dim: string;
6
+ red: string;
7
+ green: string;
8
+ yellow: string;
9
+ blue: string;
10
+ magenta: string;
11
+ cyan: string;
12
+ white: string;
13
+ gray: string;
14
+ bgRed: string;
15
+ bgGreen: string;
16
+ bgYellow: string;
17
+ };
18
+ export declare function colorize(text: string, color: keyof typeof colors): string;
19
+ export declare function success(message: string): string;
20
+ export declare function error(message: string): string;
21
+ export declare function warning(message: string): string;
22
+ export declare function info(message: string): string;
23
+ export declare function heading(text: string): string;
24
+ export declare function formatStatus(status: string): string;
25
+ export declare function formatBytes(bytes: number): string;
26
+ export declare function formatDate(dateStr: string): string;
27
+ export declare function formatDateTime(dateStr: string): string;
28
+ export declare function formatServerList(servers: {
29
+ server: Server;
30
+ }[]): string;
31
+ export declare function formatServerDetails(server: ServerDetails): string;
32
+ export declare function formatResetOptions(reset: Reset): string;
33
+ export declare function formatResetResult(reset: Reset, type: string): string;
34
+ export declare function formatBootConfig(config: BootConfig, serverNumber: number): string;
35
+ export declare function formatRescueActivation(rescue: RescueConfig): string;
36
+ export declare function formatLinuxActivation(linux: LinuxConfig): string;
37
+ export declare function formatIpList(ips: {
38
+ ip: IP;
39
+ }[]): string;
40
+ export declare function formatIpDetails(ip: IP): string;
41
+ export declare function formatSubnetList(subnets: {
42
+ subnet: Subnet;
43
+ }[]): string;
44
+ export declare function formatFailoverList(failovers: {
45
+ failover: Failover;
46
+ }[]): string;
47
+ export declare function formatFailoverSwitch(failover: Failover): string;
48
+ export declare function formatRdnsList(entries: {
49
+ rdns: Rdns;
50
+ }[]): string;
51
+ export declare function formatSshKeyList(keys: {
52
+ key: SshKey;
53
+ }[]): string;
54
+ export declare function formatSshKeyDetails(key: SshKey): string;
55
+ export declare function formatFirewall(firewall: Firewall): string;
56
+ export declare function formatFirewallTemplateList(templates: {
57
+ firewall_template: FirewallTemplate;
58
+ }[]): string;
59
+ export declare function formatVSwitchList(vswitches: {
60
+ vswitch: VSwitch;
61
+ }[]): string;
62
+ export declare function formatVSwitchDetails(vswitch: VSwitch): string;
63
+ export declare function formatStorageBoxList(boxes: {
64
+ storagebox: StorageBox;
65
+ }[]): string;
66
+ export declare function formatStorageBoxDetails(box: StorageBox): string;
67
+ export declare function formatStorageBoxSnapshots(snapshots: {
68
+ snapshot: StorageBoxSnapshot;
69
+ }[]): string;
70
+ export declare function formatStorageBoxSubaccounts(subaccounts: {
71
+ subaccount: StorageBoxSubaccount;
72
+ }[]): string;
73
+ export declare function formatTraffic(traffic: Traffic): string;
74
+ export declare function formatWolResult(wol: Wol): string;
75
+ export declare function formatServerProductList(products: {
76
+ product: ServerProduct;
77
+ }[]): string;
78
+ export declare function formatServerMarketProductList(products: {
79
+ product: ServerMarketProduct;
80
+ }[]): string;
81
+ export declare function formatTransactionList(transactions: {
82
+ transaction: ServerTransaction;
83
+ }[]): string;
84
+ export declare function formatCancellation(cancellation: Cancellation): string;
85
+ export declare function formatJson(data: unknown): string;