import * as fs from 'fs' import { initConfig, loadConfig, migrateConfigIfNeeded } from '../lib/config' // Command to initialize a new zzup.yaml file export async function cmdInit() { try { const configPath = migrateConfigIfNeeded() console.log(`Initializing zzup configuration in ${configPath}...`) // Check if file already exists if (fs.existsSync(configPath)) { console.log(`Configuration file already exists: ${configPath}`) console.log( 'Edit the configuration file directly to add or update images.' ) return 0 } // Create a new config file initConfig(configPath) console.log(`Created new configuration file: ${configPath}`) console.log(`Edit the file directly to add images in one of these formats:`) console.log(` # Standard format (with explicit source and target directories): image-name: tag: latest sourceDir: /source/dir/ targetDir: /target/dir/ # Shorthand format (using manifest from the image): image-name: "latest" # Just specify the tag `) return 0 } catch (error) { console.error(`Error initializing configuration: ${error}`) return 1 } } // Command to list configured images export async function cmdConfigList() { try { const configPath = migrateConfigIfNeeded() // Check if config file exists if (!fs.existsSync(configPath)) { console.log(`No configuration file found at ${configPath}`) console.log('Run "zzup init" to create a new configuration.') return 0 } // Load config const config = loadConfig() // Get image names const imageNames = Object.keys(config) if (imageNames.length === 0) { console.log(`No images configured in ${configPath}`) console.log('Edit the configuration file to add images.') return 0 } console.log(`Configured Images:`) console.log('') // Display each configured image imageNames.forEach((imageName, index) => { const imageConfig = config[imageName] console.log(`${imageName}:${imageConfig.tag}`) // Only show source and target if they exist if (imageConfig.sourceDir) { console.log(` Source: ${imageConfig.sourceDir}`) } if (imageConfig.targetDir) { console.log(` Target: ${imageConfig.targetDir}`) } // If neither source nor target is specified, show it's using manifest if (!imageConfig.sourceDir && !imageConfig.targetDir) { console.log(` (Using manifest from image)`) } // Add spacing between items except the last one if (index < imageNames.length - 1) { console.log('') } }) return 0 } catch (error) { console.error(`Error listing configuration: ${error}`) return 1 } }