blob: febd56792c68aceb6e9d79b6bfd3a9f585f87dbc [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 Bauera98448f2024-03-06 14:01:19 -050019use anyhow::{anyhow, ensure, Result};
Ted Bauer4dbf58a2024-02-08 18:46:52 +000020use clap::Parser;
21
22mod device_config_source;
23use device_config_source::DeviceConfigSource;
24
Ted Bauera98448f2024-03-06 14:01:19 -050025#[derive(Clone, PartialEq)]
Ted Bauer4dbf58a2024-02-08 18:46:52 +000026enum 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
Mårten Kongstadf0c594d2024-03-08 10:20:32 +010055#[derive(Clone, Copy, PartialEq, Eq, Debug)]
56enum FlagValue {
57 Enabled,
58 Disabled,
59}
60
61impl TryFrom<&str> for FlagValue {
62 type Error = anyhow::Error;
63
64 fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
65 match value {
66 "true" | "enabled" => Ok(Self::Enabled),
67 "false" | "disabled" => Ok(Self::Disabled),
68 _ => Err(anyhow!("cannot convert string '{}' to FlagValue", value)),
69 }
70 }
71}
72
73impl ToString for FlagValue {
74 fn to_string(&self) -> String {
75 match &self {
76 Self::Enabled => "enabled".into(),
77 Self::Disabled => "disabled".into(),
78 }
79 }
80}
81
Ted Bauer4dbf58a2024-02-08 18:46:52 +000082#[derive(Clone)]
83struct Flag {
84 namespace: String,
85 name: String,
86 package: String,
87 container: String,
Mårten Kongstadf0c594d2024-03-08 10:20:32 +010088 value: FlagValue,
Ted Bauer4dbf58a2024-02-08 18:46:52 +000089 permission: FlagPermission,
90 value_picked_from: ValuePickedFrom,
91}
92
Ted Bauer84883bd2024-03-04 22:45:29 +000093impl Flag {
94 fn qualified_name(&self) -> String {
95 format!("{}.{}", self.package, self.name)
96 }
97}
98
Ted Bauer4dbf58a2024-02-08 18:46:52 +000099trait FlagSource {
100 fn list_flags() -> Result<Vec<Flag>>;
Ted Bauer84883bd2024-03-04 22:45:29 +0000101 fn override_flag(namespace: &str, qualified_name: &str, value: &str) -> Result<()>;
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000102}
103
104const ABOUT_TEXT: &str = "Tool for reading and writing flags.
105
106Rows in the table from the `list` command follow this format:
107
108 package flag_name value provenance permission container
109
110 * `package`: package set for this flag in its .aconfig definition.
111 * `flag_name`: flag name, also set in definition.
112 * `value`: the value read from the flag.
113 * `provenance`: one of:
114 + `default`: the flag value comes from its build-time default.
115 + `server`: the flag value comes from a server override.
116 * `permission`: read-write or read-only.
117 * `container`: the container for the flag, configured in its definition.
118";
119
120#[derive(Parser, Debug)]
121#[clap(long_about=ABOUT_TEXT)]
122struct Cli {
123 #[clap(subcommand)]
124 command: Command,
125}
126
127#[derive(Parser, Debug)]
128enum Command {
129 /// List all aconfig flags on this device.
130 List,
Ted Bauer84883bd2024-03-04 22:45:29 +0000131
132 /// Enable an aconfig flag on this device, on the next boot.
133 Enable {
134 /// <package>.<flag_name>
135 qualified_name: String,
136 },
137
138 /// Disable an aconfig flag on this device, on the next boot.
139 Disable {
140 /// <package>.<flag_name>
141 qualified_name: String,
142 },
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000143}
144
145struct PaddingInfo {
Mårten Kongstadd408e962024-03-07 13:56:30 +0100146 longest_flag_col: usize,
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000147 longest_val_col: usize,
148 longest_value_picked_from_col: usize,
149 longest_permission_col: usize,
150}
151
152fn format_flag_row(flag: &Flag, info: &PaddingInfo) -> String {
Mårten Kongstadd408e962024-03-07 13:56:30 +0100153 let full_name = flag.qualified_name();
154 let p0 = info.longest_flag_col + 1;
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000155
Mårten Kongstadf0c594d2024-03-08 10:20:32 +0100156 let val = flag.value.to_string();
Mårten Kongstadd408e962024-03-07 13:56:30 +0100157 let p1 = info.longest_val_col + 1;
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000158
159 let value_picked_from = flag.value_picked_from.to_string();
Mårten Kongstadd408e962024-03-07 13:56:30 +0100160 let p2 = info.longest_value_picked_from_col + 1;
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000161
162 let perm = flag.permission.to_string();
Mårten Kongstadd408e962024-03-07 13:56:30 +0100163 let p3 = info.longest_permission_col + 1;
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000164
165 let container = &flag.container;
166
Mårten Kongstadd408e962024-03-07 13:56:30 +0100167 format!("{full_name:p0$}{val:p1$}{value_picked_from:p2$}{perm:p3$}{container}\n")
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000168}
169
Ted Bauer84883bd2024-03-04 22:45:29 +0000170fn set_flag(qualified_name: &str, value: &str) -> Result<()> {
Ted Bauera98448f2024-03-06 14:01:19 -0500171 ensure!(nix::unistd::Uid::current().is_root(), "must be root to mutate flags");
172
Ted Bauer84883bd2024-03-04 22:45:29 +0000173 let flags_binding = DeviceConfigSource::list_flags()?;
174 let flag = flags_binding.iter().find(|f| f.qualified_name() == qualified_name).ok_or(
175 anyhow!("no aconfig flag '{qualified_name}'. Does the flag have an .aconfig definition?"),
176 )?;
177
Ted Bauera98448f2024-03-06 14:01:19 -0500178 ensure!(flag.permission == FlagPermission::ReadWrite,
179 format!("could not write flag '{qualified_name}', it is read-only for the current release configuration."));
Ted Bauer84883bd2024-03-04 22:45:29 +0000180
181 DeviceConfigSource::override_flag(&flag.namespace, qualified_name, value)?;
182
183 Ok(())
184}
185
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000186fn list() -> Result<String> {
187 let flags = DeviceConfigSource::list_flags()?;
188 let padding_info = PaddingInfo {
Mårten Kongstadd408e962024-03-07 13:56:30 +0100189 longest_flag_col: flags.iter().map(|f| f.qualified_name().len()).max().unwrap_or(0),
Mårten Kongstadf0c594d2024-03-08 10:20:32 +0100190 longest_val_col: flags.iter().map(|f| f.value.to_string().len()).max().unwrap_or(0),
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000191 longest_value_picked_from_col: flags
192 .iter()
193 .map(|f| f.value_picked_from.to_string().len())
194 .max()
195 .unwrap_or(0),
196 longest_permission_col: flags
197 .iter()
198 .map(|f| f.permission.to_string().len())
199 .max()
200 .unwrap_or(0),
201 };
202
203 let mut result = String::from("");
204 for flag in flags {
205 let row = format_flag_row(&flag, &padding_info);
206 result.push_str(&row);
207 }
208 Ok(result)
209}
210
211fn main() {
212 let cli = Cli::parse();
213 let output = match cli.command {
Ted Bauer84883bd2024-03-04 22:45:29 +0000214 Command::List => list().map(Some),
215 Command::Enable { qualified_name } => set_flag(&qualified_name, "true").map(|_| None),
216 Command::Disable { qualified_name } => set_flag(&qualified_name, "false").map(|_| None),
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000217 };
218 match output {
Ted Bauer84883bd2024-03-04 22:45:29 +0000219 Ok(Some(text)) => println!("{text}"),
220 Ok(None) => (),
221 Err(message) => println!("Error: {message}"),
Ted Bauer4dbf58a2024-02-08 18:46:52 +0000222 }
223}