Debian Codegraph @

cli/src/commands/config/mod.rs

raw
1 // Copyright 2020 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 mod edit;
16 mod gc;
17 mod get;
18 mod list;
19 mod path;
20 mod set;
21 mod unset;
22
23 use std::path::PathBuf;
24
25 use itertools::Itertools as _;
26 use jj_lib::config::ConfigFile;
27 use jj_lib::config::ConfigSource;
28 use tracing::instrument;
29
30 use self::edit::ConfigEditArgs;
31 use self::edit::cmd_config_edit;
32 use self::gc::ConfigGcArgs;
33 use self::gc::cmd_config_gc;
34 use self::get::ConfigGetArgs;
35 use self::get::cmd_config_get;
36 use self::list::ConfigListArgs;
37 use self::list::cmd_config_list;
38 use self::path::ConfigPathArgs;
39 use self::path::cmd_config_path;
40 use self::set::ConfigSetArgs;
41 use self::set::cmd_config_set;
42 use self::unset::ConfigUnsetArgs;
43 use self::unset::cmd_config_unset;
44 use crate::cli_util::CommandHelper;
45 use crate::command_error::CommandError;
46 use crate::command_error::user_error;
47 use crate::config::ConfigEnv;
48 use crate::ui::Ui;
49
50 #[derive(clap::Args, Clone, Debug)]
51 #[group(id = "config_level", multiple = false, required = true)]
52 pub(crate) struct ConfigLevelArgs {
53 /// Target the user-level config
54 #[arg(long)]
55 user: bool,
56
57 /// Target the repo-level config
58 #[arg(long)]
59 repo: bool,
60
61 /// Target the workspace-level config
62 #[arg(long)]
63 workspace: bool,
64 }
65
66 impl ConfigLevelArgs {
67 fn get_source_kind(&self) -> Option<ConfigSource> {
68 if self.user {
69 Some(ConfigSource::User)
70 } else if self.repo {
71 Some(ConfigSource::Repo)
72 } else if self.workspace {
73 Some(ConfigSource::Workspace)
74 } else {
75 None
76 }
77 }
78
79 fn config_paths(&self, ui: &Ui, config_env: &ConfigEnv) -> Result<Vec<PathBuf>, CommandError> {
80 if self.user {
81 let paths = config_env
82 .user_config_paths()
83 .map(|p| p.to_path_buf())
84 .collect_vec();
85 if paths.is_empty() {
86 return Err(user_error("No user config path found"));
87 }
88 Ok(paths)
89 } else if self.repo {
90 config_env
91 .repo_config_path(ui)?
92 .map(|p| vec![p])
93 .ok_or_else(|| user_error("No repo config path found"))
94 } else if self.workspace {
95 config_env
96 .workspace_config_path(ui)?
97 .map(|p| vec![p])
98 .ok_or_else(|| user_error("No workspace config path found"))
99 } else {
100 panic!("No config_level provided")
101 }
102 }
103 }
104
105 #[derive(clap::Args, Clone, Debug)]
106 #[group(id = "config_target", multiple = false, required = true)]
107 pub(crate) struct ConfigTargetArgs {
108 /// Target the user-level config
109 #[arg(long)]
110 user: bool,
111
112 /// Target the repo-level config
113 #[arg(long)]
114 repo: bool,
115
116 /// Target the workspace-level config
117 #[arg(long)]
118 workspace: bool,
119
120 /// Target the config file specified by the given path
121 ///
122 /// The path must point to a valid configuration file location recognized
123 /// by Jujutsu (such as a user/repo/workspace config, a file inside a
124 /// `conf.d/` directory, or any file loaded via system configs,
125 /// `$JJ_CONFIG`, or `--config-file`).
126 ///
127 /// Unlike the global `--config-file` option (which loads an extra config
128 /// file when running commands), this option specifies which file to
129 /// inspect, edit, or modify on disk.
130 ///
131 /// If the file does not exist, commands like `set` and `edit` will create
132 /// it and any missing parent directories.
133 #[arg(long, value_name = "PATH", value_hint = clap::ValueHint::FilePath)]
134 file: Option<PathBuf>,
135 }
136
137 impl ConfigTargetArgs {
138 fn edit_config_file(
139 &self,
140 ui: &Ui,
141 command: &CommandHelper,
142 ) -> Result<ConfigFile, CommandError> {
143 let config_env = command.config_env();
144 let config = command.raw_config();
145 let pick_first = |files: Vec<ConfigFile>, not_found_error: &str| {
146 files
147 .into_iter()
148 .next()
149 .ok_or_else(|| user_error(not_found_error))
150 };
151 if let Some(file) = &self.file {
152 let path = command.cwd().join(file);
153 config_env.resolve_file_to_edit(ui, config, &path)
154 } else if self.user {
155 pick_first(
156 config_env.user_config_files(config)?,
157 "No user config path found to edit",
158 )
159 } else if self.repo {
160 pick_first(
161 config_env.repo_config_files(ui, config)?,
162 "No repo config path found to edit",
163 )
164 } else if self.workspace {
165 pick_first(
166 config_env.workspace_config_files(ui, config)?,
167 "No workspace config path found to edit",
168 )
169 } else {
170 panic!("No config_target provided")
171 }
172 }
173 }
174
175 /// Manage config options
176 ///
177 /// Operates on jj configuration, which comes from the config file and
178 /// environment variables.
179 ///
180 /// See [`jj help -k config`] to know more about file locations, supported
181 /// config options, and other details about `jj config`.
182 ///
183 /// [`jj help -k config`]:
184 /// https://docs.jj-vcs.dev/latest/config/
185 #[derive(clap::Subcommand, Clone, Debug)]
186 pub(crate) enum ConfigCommand {
187 #[command(visible_alias("e"))]
188 Edit(ConfigEditArgs),
189 Gc(ConfigGcArgs),
190 #[command(visible_alias("g"))]
191 Get(ConfigGetArgs),
192 #[command(visible_alias("l"))]
193 List(ConfigListArgs),
194 #[command(visible_alias("p"))]
195 Path(ConfigPathArgs),
196 #[command(visible_alias("s"))]
197 Set(ConfigSetArgs),
198 #[command(visible_alias("u"))]
199 Unset(ConfigUnsetArgs),
200 }
201
202 #[instrument(skip_all)]
203 pub(crate) async fn cmd_config(
204 ui: &mut Ui,
205 command: &CommandHelper,
206 subcommand: &ConfigCommand,
207 ) -> Result<(), CommandError> {
208 match subcommand {
209 ConfigCommand::Edit(args) => cmd_config_edit(ui, command, args).await,
210 ConfigCommand::Gc(args) => cmd_config_gc(ui, command, args).await,
211 ConfigCommand::Get(args) => cmd_config_get(ui, command, args).await,
212 ConfigCommand::List(args) => cmd_config_list(ui, command, args).await,
213 ConfigCommand::Path(args) => cmd_config_path(ui, command, args).await,
214 ConfigCommand::Set(args) => cmd_config_set(ui, command, args).await,
215 ConfigCommand::Unset(args) => cmd_config_unset(ui, command, args).await,
216 }
217 }