Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 1 | /* |
| 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 Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 19 | use anyhow::{anyhow, Result}; |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 20 | use clap::Parser; |
| 21 | |
| 22 | mod device_config_source; |
| 23 | use device_config_source::DeviceConfigSource; |
| 24 | |
| 25 | #[derive(Clone)] |
| 26 | enum FlagPermission { |
| 27 | ReadOnly, |
| 28 | ReadWrite, |
| 29 | } |
| 30 | |
| 31 | impl 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)] |
| 41 | enum ValuePickedFrom { |
| 42 | Default, |
| 43 | Server, |
| 44 | } |
| 45 | |
| 46 | impl 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)] |
| 56 | struct 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 Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 66 | impl Flag { |
| 67 | fn qualified_name(&self) -> String { |
| 68 | format!("{}.{}", self.package, self.name) |
| 69 | } |
| 70 | } |
| 71 | |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 72 | trait FlagSource { |
| 73 | fn list_flags() -> Result<Vec<Flag>>; |
Ted Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 74 | fn override_flag(namespace: &str, qualified_name: &str, value: &str) -> Result<()>; |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | const ABOUT_TEXT: &str = "Tool for reading and writing flags. |
| 78 | |
| 79 | Rows 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)] |
| 95 | struct Cli { |
| 96 | #[clap(subcommand)] |
| 97 | command: Command, |
| 98 | } |
| 99 | |
| 100 | #[derive(Parser, Debug)] |
| 101 | enum Command { |
| 102 | /// List all aconfig flags on this device. |
| 103 | List, |
Ted Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 104 | |
| 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 Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | struct PaddingInfo { |
Mårten Kongstad | d408e96 | 2024-03-07 13:56:30 +0100 | [diff] [blame^] | 119 | longest_flag_col: usize, |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 120 | longest_val_col: usize, |
| 121 | longest_value_picked_from_col: usize, |
| 122 | longest_permission_col: usize, |
| 123 | } |
| 124 | |
| 125 | fn format_flag_row(flag: &Flag, info: &PaddingInfo) -> String { |
Mårten Kongstad | d408e96 | 2024-03-07 13:56:30 +0100 | [diff] [blame^] | 126 | let full_name = flag.qualified_name(); |
| 127 | let p0 = info.longest_flag_col + 1; |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 128 | |
| 129 | let val = flag.value.to_string(); |
Mårten Kongstad | d408e96 | 2024-03-07 13:56:30 +0100 | [diff] [blame^] | 130 | let p1 = info.longest_val_col + 1; |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 131 | |
| 132 | let value_picked_from = flag.value_picked_from.to_string(); |
Mårten Kongstad | d408e96 | 2024-03-07 13:56:30 +0100 | [diff] [blame^] | 133 | let p2 = info.longest_value_picked_from_col + 1; |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 134 | |
| 135 | let perm = flag.permission.to_string(); |
Mårten Kongstad | d408e96 | 2024-03-07 13:56:30 +0100 | [diff] [blame^] | 136 | let p3 = info.longest_permission_col + 1; |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 137 | |
| 138 | let container = &flag.container; |
| 139 | |
Mårten Kongstad | d408e96 | 2024-03-07 13:56:30 +0100 | [diff] [blame^] | 140 | format!("{full_name:p0$}{val:p1$}{value_picked_from:p2$}{perm:p3$}{container}\n") |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Ted Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 143 | fn 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 Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 160 | fn list() -> Result<String> { |
| 161 | let flags = DeviceConfigSource::list_flags()?; |
| 162 | let padding_info = PaddingInfo { |
Mårten Kongstad | d408e96 | 2024-03-07 13:56:30 +0100 | [diff] [blame^] | 163 | longest_flag_col: flags.iter().map(|f| f.qualified_name().len()).max().unwrap_or(0), |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 164 | 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 | |
| 185 | fn main() { |
| 186 | let cli = Cli::parse(); |
| 187 | let output = match cli.command { |
Ted Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 188 | 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 Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 191 | }; |
| 192 | match output { |
Ted Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 193 | Ok(Some(text)) => println!("{text}"), |
| 194 | Ok(None) => (), |
| 195 | Err(message) => println!("Error: {message}"), |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 196 | } |
| 197 | } |