001/* 002 * VM-Operator 003 * Copyright (C) 2023 Michael N. Lipp 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <https://www.gnu.org/licenses/>. 017 */ 018 019package org.jdrupes.vmoperator.runner.qemu; 020 021import com.fasterxml.jackson.databind.JsonNode; 022import java.nio.file.Files; 023import java.nio.file.Path; 024import java.util.ArrayList; 025import java.util.List; 026 027/** 028 * A command definition. 029 */ 030class CommandDefinition { 031 public String name; 032 public final List<String> command = new ArrayList<>(); 033 034 /** 035 * Instantiates a new process definition. 036 * 037 * @param name the name 038 * @param jsonData the json data 039 */ 040 public CommandDefinition(String name, JsonNode jsonData) { 041 this.name = name; 042 for (JsonNode path : jsonData.get("executable")) { 043 if (Files.isExecutable(Path.of(path.asText()))) { 044 command.add(path.asText()); 045 break; 046 } 047 } 048 if (command.isEmpty()) { 049 throw new IllegalArgumentException("No executable found."); 050 } 051 collect(command, jsonData.get("arguments")); 052 } 053 054 private void collect(List<String> result, JsonNode node) { 055 if (!node.isArray()) { 056 result.add(node.asText()); 057 return; 058 } 059 for (var element : node) { 060 collect(result, element); 061 } 062 } 063 064 /** 065 * Returns the name. 066 * 067 * @return the string 068 */ 069 public String name() { 070 return name; 071 } 072 073 @Override 074 public String toString() { 075 return "Command " + name + ": " + command; 076 } 077}