@tolgamorf/env2op-cli 0.1.1 → 0.1.2
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/package.json +1 -1
- package/src/commands/convert.ts +19 -14
- package/src/core/onepassword.ts +12 -0
- package/src/core/template-generator.ts +5 -3
- package/src/core/types.ts +10 -4
package/package.json
CHANGED
package/src/commands/convert.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
vaultExists,
|
|
12
12
|
} from "../core/onepassword";
|
|
13
13
|
import { generateTemplateContent, generateUsageInstructions, writeTemplate } from "../core/template-generator";
|
|
14
|
-
import type { ConvertOptions } from "../core/types";
|
|
14
|
+
import type { ConvertOptions, CreateItemResult } from "../core/types";
|
|
15
15
|
import { Env2OpError } from "../utils/errors";
|
|
16
16
|
import { logger } from "../utils/logger";
|
|
17
17
|
|
|
@@ -41,6 +41,8 @@ export async function runConvert(options: ConvertOptions): Promise<void> {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
// Step 2: Create 1Password Secure Note
|
|
44
|
+
let itemResult: CreateItemResult | null = null;
|
|
45
|
+
|
|
44
46
|
if (dryRun) {
|
|
45
47
|
logger.warn("Would create Secure Note");
|
|
46
48
|
logger.keyValue("Vault", vault);
|
|
@@ -70,7 +72,9 @@ export async function runConvert(options: ConvertOptions): Promise<void> {
|
|
|
70
72
|
// Check if vault exists
|
|
71
73
|
const vaultFound = await vaultExists(vault);
|
|
72
74
|
|
|
73
|
-
if (
|
|
75
|
+
if (vaultFound) {
|
|
76
|
+
logger.success(`Vault "${vault}" found`);
|
|
77
|
+
} else {
|
|
74
78
|
if (yes) {
|
|
75
79
|
// Auto-create vault
|
|
76
80
|
logger.warn(`Vault "${vault}" not found, creating...`);
|
|
@@ -122,14 +126,14 @@ export async function runConvert(options: ConvertOptions): Promise<void> {
|
|
|
122
126
|
spinner.start("Creating 1Password Secure Note...");
|
|
123
127
|
|
|
124
128
|
try {
|
|
125
|
-
|
|
129
|
+
itemResult = await createSecureNote({
|
|
126
130
|
vault,
|
|
127
131
|
title: itemName,
|
|
128
132
|
fields: variables,
|
|
129
133
|
secret,
|
|
130
134
|
});
|
|
131
135
|
|
|
132
|
-
spinner.stop(`Created "${
|
|
136
|
+
spinner.stop(`Created "${itemResult.title}" in vault "${itemResult.vault}"`);
|
|
133
137
|
} catch (error) {
|
|
134
138
|
spinner.stop("Failed to create Secure Note");
|
|
135
139
|
throw error;
|
|
@@ -139,19 +143,20 @@ export async function runConvert(options: ConvertOptions): Promise<void> {
|
|
|
139
143
|
// Step 3: Generate template file
|
|
140
144
|
const templateFileName = `${basename(envFile)}.tpl`;
|
|
141
145
|
const templatePath = join(dirname(envFile), templateFileName);
|
|
142
|
-
const templateContent = generateTemplateContent(
|
|
143
|
-
{
|
|
144
|
-
vault,
|
|
145
|
-
itemTitle: itemName,
|
|
146
|
-
variables,
|
|
147
|
-
lines,
|
|
148
|
-
},
|
|
149
|
-
templateFileName,
|
|
150
|
-
);
|
|
151
146
|
|
|
152
147
|
if (dryRun) {
|
|
153
148
|
logger.warn(`Would generate template: ${templatePath}`);
|
|
154
|
-
} else {
|
|
149
|
+
} else if (itemResult) {
|
|
150
|
+
const templateContent = generateTemplateContent(
|
|
151
|
+
{
|
|
152
|
+
vaultId: itemResult.vaultId,
|
|
153
|
+
itemId: itemResult.id,
|
|
154
|
+
variables,
|
|
155
|
+
lines,
|
|
156
|
+
fieldIds: itemResult.fieldIds,
|
|
157
|
+
},
|
|
158
|
+
templateFileName,
|
|
159
|
+
);
|
|
155
160
|
writeTemplate(templateContent, templatePath);
|
|
156
161
|
logger.success(`Generated template: ${templatePath}`);
|
|
157
162
|
}
|
package/src/core/onepassword.ts
CHANGED
|
@@ -76,10 +76,22 @@ export async function createSecureNote(options: CreateItemOptions): Promise<Crea
|
|
|
76
76
|
// Execute op command
|
|
77
77
|
const result = await $`op ${args}`.json();
|
|
78
78
|
|
|
79
|
+
// Extract field IDs mapped by label
|
|
80
|
+
const fieldIds: Record<string, string> = {};
|
|
81
|
+
if (Array.isArray(result.fields)) {
|
|
82
|
+
for (const field of result.fields) {
|
|
83
|
+
if (field.label && field.id) {
|
|
84
|
+
fieldIds[field.label] = field.id;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
79
89
|
return {
|
|
80
90
|
id: result.id,
|
|
81
91
|
title: result.title,
|
|
82
92
|
vault: result.vault?.name ?? vault,
|
|
93
|
+
vaultId: result.vault?.id ?? "",
|
|
94
|
+
fieldIds,
|
|
83
95
|
};
|
|
84
96
|
} catch (error) {
|
|
85
97
|
const message = error instanceof Error ? error.message : String(error);
|
|
@@ -12,7 +12,7 @@ import type { TemplateOptions } from "./types";
|
|
|
12
12
|
* - `op run --env-file template.tpl -- command`
|
|
13
13
|
*/
|
|
14
14
|
export function generateTemplateContent(options: TemplateOptions, templateFileName: string): string {
|
|
15
|
-
const {
|
|
15
|
+
const { vaultId, itemId, lines: envLines, fieldIds } = options;
|
|
16
16
|
|
|
17
17
|
const outputLines: string[] = [
|
|
18
18
|
`# Generated by env2op v${pkg.version}`,
|
|
@@ -32,9 +32,11 @@ export function generateTemplateContent(options: TemplateOptions, templateFileNa
|
|
|
32
32
|
case "comment":
|
|
33
33
|
outputLines.push(line.content);
|
|
34
34
|
break;
|
|
35
|
-
case "variable":
|
|
36
|
-
|
|
35
|
+
case "variable": {
|
|
36
|
+
const fieldId = fieldIds[line.key] ?? line.key;
|
|
37
|
+
outputLines.push(`${line.key}=op://${vaultId}/${itemId}/${fieldId}`);
|
|
37
38
|
break;
|
|
39
|
+
}
|
|
38
40
|
}
|
|
39
41
|
}
|
|
40
42
|
|
package/src/core/types.ts
CHANGED
|
@@ -56,6 +56,10 @@ export interface CreateItemResult {
|
|
|
56
56
|
title: string;
|
|
57
57
|
/** Vault name */
|
|
58
58
|
vault: string;
|
|
59
|
+
/** Vault ID */
|
|
60
|
+
vaultId: string;
|
|
61
|
+
/** Field IDs mapped by field label */
|
|
62
|
+
fieldIds: Record<string, string>;
|
|
59
63
|
}
|
|
60
64
|
|
|
61
65
|
/**
|
|
@@ -80,12 +84,14 @@ export interface ConvertOptions {
|
|
|
80
84
|
* Options for template generation
|
|
81
85
|
*/
|
|
82
86
|
export interface TemplateOptions {
|
|
83
|
-
/** Vault
|
|
84
|
-
|
|
85
|
-
/** Item
|
|
86
|
-
|
|
87
|
+
/** Vault ID */
|
|
88
|
+
vaultId: string;
|
|
89
|
+
/** Item ID in 1Password */
|
|
90
|
+
itemId: string;
|
|
87
91
|
/** Variables to include */
|
|
88
92
|
variables: EnvVariable[];
|
|
89
93
|
/** All lines preserving structure */
|
|
90
94
|
lines: EnvLine[];
|
|
95
|
+
/** Field IDs mapped by field label */
|
|
96
|
+
fieldIds: Record<string, string>;
|
|
91
97
|
}
|