1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::path::PathBuf;
use std::vec;

use serde::{Deserialize, Serialize};
use tracing::{debug, warn};

use crate::error::Error;

#[derive(Debug, Default, Deserialize, Serialize, derive_builder::Builder)]
#[allow(unused)]
#[non_exhaustive]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[builder(pattern = "owned", default)]
pub struct Settings {
  #[serde(default)]
  /// Logging configuration.
  pub trace: TraceSettings,
  /// Registry credentials.
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub credentials: Vec<Credential>,
  #[serde(skip)]
  /// Where this configuration was loaded from.
  pub source: Option<PathBuf>,
}

#[derive(Debug, Default, Deserialize, Serialize, Clone)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
/// Logging configuration.
pub struct TraceSettings {
  /// OTLP endpoint endpoint.
  #[serde(default, skip_serializing_if = "Option::is_none")]
  pub otlp: Option<String>,
  /// Logging level.
  #[serde(default)]
  pub level: LogLevel,
  /// Logging modifier.
  #[serde(default)]
  pub modifier: LogModifier,
  /// Telemetry filter settings.
  #[serde(default, skip_serializing_if = "Option::is_none")]
  pub telemetry: Option<LogSettings>,
  /// STDERR logging settings.
  #[serde(default, skip_serializing_if = "Option::is_none")]
  pub stderr: Option<LogSettings>,
}

#[derive(Debug, Default, Deserialize, Serialize, Clone)]
#[non_exhaustive]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
/// Log filter settings
pub struct LogSettings {
  /// Log event filter.
  pub filter: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
#[non_exhaustive]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
/// Registry credentials.
pub struct Credential {
  /// Registry URL.
  pub scope: String,
  /// Authentication method.
  pub auth: Auth,
}

impl Credential {
  /// Create a new credential entry.
  #[must_use]
  pub const fn new(scope: String, auth: Auth) -> Self {
    Self { scope, auth }
  }
}

#[derive(Debug, Deserialize, Serialize)]
#[allow(clippy::exhaustive_enums)]
#[serde(rename_all = "snake_case")]
#[serde(tag = "type")]
/// Authentication methods.
pub enum Auth {
  /// Basic authentication.
  Basic(BasicAuth),
}

#[derive(Deserialize, Serialize)]
#[non_exhaustive]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "snake_case")]
/// Basic authentication.
pub struct BasicAuth {
  /// Username.
  pub username: String,
  /// Password.
  pub password: String,
}

impl BasicAuth {
  /// Create a new basic authentication entry.
  #[must_use]
  pub const fn new(username: String, password: String) -> Self {
    Self { username, password }
  }
}

impl std::fmt::Debug for BasicAuth {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("BasicAuth")
      .field("username", &self.username)
      .field("password", &"<HIDDEN>")
      .finish()
  }
}

#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq)]
#[allow(clippy::exhaustive_enums)]
#[serde(rename_all = "snake_case")]
/// Logging levels.
pub enum LogLevel {
  /// Disable loging
  Off,
  /// Errors only.
  Error,
  /// Warnings and errors only.
  Warn,
  /// Info-level logging.
  Info,
  /// Debug logging.
  Debug,
  /// Trace logging.
  Trace,
}

impl Default for LogLevel {
  fn default() -> Self {
    Self::Info
  }
}

#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq)]
#[allow(clippy::exhaustive_enums)]
#[serde(rename_all = "snake_case")]
/// Logging modifiers.
pub enum LogModifier {
  /// No modifiers.
  None,
  /// Additional location and file data.
  Verbose,
}

impl Default for LogModifier {
  fn default() -> Self {
    Self::None
  }
}

impl Settings {
  pub fn new() -> Self {
    let _span = tracing::info_span!("settings").entered();
    let extensions = vec!["yaml", "yml"];

    let xdg = wick_xdg::Settings::new();

    let config_locations = vec![
      xdg.config_dir().join(xdg.configfile_basename()),
      PathBuf::from(xdg.configfile_basename()),
    ];

    tracing::debug!(
      paths = ?config_locations.iter().map(|v| format!("{}.({})",v.display(),extensions.join(","))).collect::<Vec<_>>(),
      "searching for config files"
    );

    let mut files = find_settings(&config_locations, &extensions);

    debug!("loaded");

    if !files.is_empty() {
      files.remove(0)
    } else {
      Self::default()
    }
  }

  pub fn save(&self) -> Result<(), Error> {
    let source = self.source.as_ref().ok_or(Error::NoSource)?;
    let yaml = serde_yaml::to_string(self).unwrap();
    std::fs::write(source, yaml).map_err(|e| Error::SaveFailed(source.clone(), e))?;
    Ok(())
  }
}

#[allow(clippy::cognitive_complexity)]
fn find_settings(config_locations: &[PathBuf], extensions: &[&str]) -> Vec<Settings> {
  let mut files = Vec::new();
  for path in config_locations {
    for ext in extensions {
      let mut path = path.clone();
      path.set_extension(ext);
      if path.exists() {
        match std::fs::read_to_string(&path) {
          Ok(src) => match serde_yaml::from_str::<Settings>(&src) {
            Ok(mut settings) => {
              debug!(file=%path.display(),"found config");
              settings.source = Some(path.clone());
              files.push(settings);

              break; // only load the first one, fix when merging is implemented.
            }
            Err(e) => {
              warn!(error=%e,file=%path.display(),"failed to parse config");
            }
          },
          Err(e) => {
            warn!(error=%e,file=%path.display(),"failed to read config");
          }
        };
      }
    }
  }
  files
}