|
| 1 | +/* |
| 2 | + * Copyright (c) 2004-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +#include "fboss/cli/fboss2/commands/config/session/CmdConfigSessionDiff.h" |
| 12 | +#include "fboss/cli/fboss2/session/ConfigSession.h" |
| 13 | + |
| 14 | +#include <folly/Subprocess.h> |
| 15 | +#include <filesystem> |
| 16 | + |
| 17 | +namespace fs = std::filesystem; |
| 18 | + |
| 19 | +namespace facebook::fboss { |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +// Helper function to resolve a revision specifier to a file path |
| 24 | +// Note: Revision format validation is done in RevisionList constructor |
| 25 | +std::string resolveRevisionPath( |
| 26 | + const std::string& revision, |
| 27 | + const std::string& cliConfigDir, |
| 28 | + const std::string& systemConfigPath) { |
| 29 | + if (revision == "current") { |
| 30 | + return systemConfigPath; |
| 31 | + } |
| 32 | + |
| 33 | + // Build the path (revision is already validated to be in "rN" format) |
| 34 | + std::string revisionPath = cliConfigDir + "/agent-" + revision + ".conf"; |
| 35 | + |
| 36 | + // Check if the file exists |
| 37 | + if (!fs::exists(revisionPath)) { |
| 38 | + throw std::invalid_argument( |
| 39 | + "Revision " + revision + " does not exist at " + revisionPath); |
| 40 | + } |
| 41 | + |
| 42 | + return revisionPath; |
| 43 | +} |
| 44 | + |
| 45 | +// Helper function to execute diff and return the result |
| 46 | +std::string executeDiff( |
| 47 | + const std::string& path1, |
| 48 | + const std::string& path2, |
| 49 | + const std::string& label1, |
| 50 | + const std::string& label2) { |
| 51 | + try { |
| 52 | + folly::Subprocess proc( |
| 53 | + std::vector<std::string>{ |
| 54 | + "/usr/bin/diff", |
| 55 | + "-u", |
| 56 | + "--label", |
| 57 | + label1, |
| 58 | + "--label", |
| 59 | + label2, |
| 60 | + path1, |
| 61 | + path2}, |
| 62 | + folly::Subprocess::Options().pipeStdout().pipeStderr()); |
| 63 | + |
| 64 | + auto result = proc.communicate(); |
| 65 | + int returnCode = proc.wait().exitStatus(); |
| 66 | + |
| 67 | + // diff returns 0 if files are identical, 1 if different, 2 on error |
| 68 | + if (returnCode == 0) { |
| 69 | + return "No differences between " + label1 + " and " + label2 + "."; |
| 70 | + } else if (returnCode == 1) { |
| 71 | + // Files differ - return the diff output |
| 72 | + return result.first; |
| 73 | + } else { |
| 74 | + // Error occurred |
| 75 | + throw std::runtime_error("diff command failed: " + result.second); |
| 76 | + } |
| 77 | + } catch (const std::exception& ex) { |
| 78 | + throw std::runtime_error( |
| 79 | + "Failed to execute diff command: " + std::string(ex.what())); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace |
| 84 | + |
| 85 | +CmdConfigSessionDiffTraits::RetType CmdConfigSessionDiff::queryClient( |
| 86 | + const HostInfo& /* hostInfo */, |
| 87 | + const utils::RevisionList& revisions) { |
| 88 | + auto& session = ConfigSession::getInstance(); |
| 89 | + |
| 90 | + std::string systemConfigPath = session.getSystemConfigPath(); |
| 91 | + std::string sessionConfigPath = session.getSessionConfigPath(); |
| 92 | + std::string cliConfigDir = session.getCliConfigDir(); |
| 93 | + |
| 94 | + // Mode 1: No arguments - diff session vs current live config |
| 95 | + if (revisions.empty()) { |
| 96 | + if (!session.sessionExists()) { |
| 97 | + return "No config session exists. Make a config change first."; |
| 98 | + } |
| 99 | + |
| 100 | + return executeDiff( |
| 101 | + systemConfigPath, |
| 102 | + sessionConfigPath, |
| 103 | + "current live config", |
| 104 | + "session config"); |
| 105 | + } |
| 106 | + |
| 107 | + // Mode 2: One argument - diff session vs specified revision |
| 108 | + if (revisions.size() == 1) { |
| 109 | + if (!session.sessionExists()) { |
| 110 | + return "No config session exists. Make a config change first."; |
| 111 | + } |
| 112 | + |
| 113 | + std::string revisionPath = |
| 114 | + resolveRevisionPath(revisions[0], cliConfigDir, systemConfigPath); |
| 115 | + std::string label = |
| 116 | + revisions[0] == "current" ? "current live config" : revisions[0]; |
| 117 | + |
| 118 | + return executeDiff( |
| 119 | + revisionPath, sessionConfigPath, label, "session config"); |
| 120 | + } |
| 121 | + |
| 122 | + // Mode 3: Two arguments - diff between two revisions |
| 123 | + if (revisions.size() == 2) { |
| 124 | + std::string path1 = |
| 125 | + resolveRevisionPath(revisions[0], cliConfigDir, systemConfigPath); |
| 126 | + std::string path2 = |
| 127 | + resolveRevisionPath(revisions[1], cliConfigDir, systemConfigPath); |
| 128 | + |
| 129 | + std::string label1 = |
| 130 | + revisions[0] == "current" ? "current live config" : revisions[0]; |
| 131 | + std::string label2 = |
| 132 | + revisions[1] == "current" ? "current live config" : revisions[1]; |
| 133 | + |
| 134 | + return executeDiff(path1, path2, label1, label2); |
| 135 | + } |
| 136 | + |
| 137 | + // More than 2 arguments is an error |
| 138 | + throw std::invalid_argument( |
| 139 | + "Too many arguments. Expected 0, 1, or 2 revision specifiers."); |
| 140 | +} |
| 141 | + |
| 142 | +void CmdConfigSessionDiff::printOutput(const RetType& diffOutput) { |
| 143 | + std::cout << diffOutput; |
| 144 | + if (!diffOutput.empty() && diffOutput.back() != '\n') { |
| 145 | + std::cout << std::endl; |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +} // namespace facebook::fboss |
0 commit comments