blob: d5704e830119415a37f4bafaa4b2a76261a345fb [file] [log] [blame]
Ted Bauer4dbf58a2024-02-08 18:46:52 +00001/*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! `aflags` is a device binary to read and write aconfig flags.
18
Ted Bauer84883bd2024-03-04 22:45:29 +000019use anyhow::{anyhow, Result};
Ted Bauer4dbf58a2024-02-08 18:46:52 +000020use clap::Parser;
21
22mod device_config_source;
23use device_config_source::DeviceConfigSource;
24
25#[derive(Clone)]
26enum FlagPermission {
27 ReadOnly,
28 ReadWrite,
29}
30
31impl ToString for FlagPermission {
32 fn to_string(&self) -> String {
33 match &self {
34 Self::ReadOnly => "read-only".into(),
35 Self::ReadWrite => "read-write".into(),
36 }
37 }
38}
39
40#[derive(Clone)]
41enum ValuePickedFrom {
42 Default,
43 Server,
44}
45
46impl ToString for ValuePickedFrom {
47 fn to_string(&self) -> String {
48 match &self {
49 Self::Default => "default".into(),
50 Self::Server => "server".into(),
51 }
52 }
53}
54
55#[derive(Clone)]
56struct Flag {
57 namespace: String,
58 name: String,
59 package: String,
60 container: String,
61 value: String,
62 permission: FlagPermission,
63 value_picked_from: ValuePickedFrom,
64}
65
Ted Bauer84883bd2024-03-04 22:45:29 +000066impl Flag {
67 fn qualified_name(&self) -> String {
68 format!("{}.{}", self.package, self.name)
69 }
70}
71
Ted Bauer4dbf58a2024-02-08 18:46:52 +000072trait FlagSource {
73 fn list_flags() -> Result<Vec<Flag>>;
Ted Bauer84883bd2024-03-04 22:45:29 +000074 fn override_flag(namespace: &str, qualified_name: &str, value: &str) -> Result<()>;
Ted Bauer4dbf58a2024-02-08 18:46:52 +000075}
76
77const ABOUT_TEXT: &str = "Tool for reading and writing flags.
78
79Rows in the table from the `list` command follow this format:
80
81 package flag_name value provenance permission container
82
83 * `package`: package set for this flag in its .aconfig definition.
84 * `flag_name`: flag name, also set in definition.
85 * `value`: the value read from the flag.
86 * `provenance`: one of:
87 + `default`: the flag value comes from its build-time default.
88 + `server`: the flag value comes from a server override.
89 * `permission`: read-write or read-only.
90 * `container`: the container for the flag, configured in its definition.
91";
92
93#[derive(Parser, Debug)]
94#[clap(long_about=ABOUT_TEXT)]
95struct Cli {
96 #[clap(subcommand)]
97 command: Command,
98}
99
100#[derive(Parser, Debug)]
101enum Command {
102 /// List all aconfig flags on this device.
103 List,
Ted Bauer84883bd2024-03-04 22:45:29 +0000104
105 /// Enable an aconfig flag on this device, on the next boot.
106 Enable {
107 /// <package>.<flag_name>
108 qualified_name: String,
109 },
110
111 /// Disable an aconfig flag on this device, on the next boot.
112 Disable {
113 /// <package>.<flag_name>
114 qualified_name: String,
115 },
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000116}
117
118struct PaddingInfo {
Mårten Kongstadd408e962024-03-07 13:56:30 +0100119 longest_flag_col: usize,
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000120 longest_val_col: usize,
121 longest_value_picked_from_col: usize,
122 longest_permission_col: usize,
123}
124
125fn format_flag_row(flag: &Flag, info: &PaddingInfo) -> String {
Mårten Kongstadd408e962024-03-07 13:56:30 +0100126 let full_name = flag.qualified_name();
127 let p0 = info.longest_flag_col + 1;
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000128
129 let val = flag.value.to_string();
Mårten Kongstadd408e962024-03-07 13:56:30 +0100130 let p1 = info.longest_val_col + 1;
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000131
132 let value_picked_from = flag.value_picked_from.to_string();
Mårten Kongstadd408e962024-03-07 13:56:30 +0100133 let p2 = info.longest_value_picked_from_col + 1;
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000134
135 let perm = flag.permission.to_string();
Mårten Kongstadd408e962024-03-07 13:56:30 +0100136 let p3 = info.longest_permission_col + 1;
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000137
138 let container = &flag.container;
139
Mårten Kongstadd408e962024-03-07 13:56:30 +0100140 format!("{full_name:p0$}{val:p1$}{value_picked_from:p2$}{perm:p3$}{container}\n")
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000141}
142
Ted Bauer84883bd2024-03-04 22:45:29 +0000143fn set_flag(qualified_name: &str, value: &str) -> Result<()> {
144 let flags_binding = DeviceConfigSource::list_flags()?;
145 let flag = flags_binding.iter().find(|f| f.qualified_name() == qualified_name).ok_or(
146 anyhow!("no aconfig flag '{qualified_name}'. Does the flag have an .aconfig definition?"),
147 )?;
148
149 if let FlagPermission::ReadOnly = flag.permission {
150 return Err(anyhow!(
151 "could not write flag '{qualified_name}', it is read-only for the current release configuration.",
152 ));
153 }
154
155 DeviceConfigSource::override_flag(&flag.namespace, qualified_name, value)?;
156
157 Ok(())
158}
159
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000160fn list() -> Result<String> {
161 let flags = DeviceConfigSource::list_flags()?;
162 let padding_info = PaddingInfo {
Mårten Kongstadd408e962024-03-07 13:56:30 +0100163 longest_flag_col: flags.iter().map(|f| f.qualified_name().len()).max().unwrap_or(0),
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000164 longest_val_col: flags.iter().map(|f| f.value.to_string().len()).max().unwrap_or(0),
165 longest_value_picked_from_col: flags
166 .iter()
167 .map(|f| f.value_picked_from.to_string().len())
168 .max()
169 .unwrap_or(0),
170 longest_permission_col: flags
171 .iter()
172 .map(|f| f.permission.to_string().len())
173 .max()
174 .unwrap_or(0),
175 };
176
177 let mut result = String::from("");
178 for flag in flags {
179 let row = format_flag_row(&flag, &padding_info);
180 result.push_str(&row);
181 }
182 Ok(result)
183}
184
185fn main() {
186 let cli = Cli::parse();
187 let output = match cli.command {
Ted Bauer84883bd2024-03-04 22:45:29 +0000188 Command::List => list().map(Some),
189 Command::Enable { qualified_name } => set_flag(&qualified_name, "true").map(|_| None),
190 Command::Disable { qualified_name } => set_flag(&qualified_name, "false").map(|_| None),
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000191 };
192 match output {
Ted Bauer84883bd2024-03-04 22:45:29 +0000193 Ok(Some(text)) => println!("{text}"),
194 Ok(None) => (),
195 Err(message) => println!("Error: {message}"),
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000196 }
197}