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
use crate::shell::{ColorStrategy, ShellName};
use clap::Parser;
use std::{io::Write, path::PathBuf};

/// Sauce!
#[derive(Parser, Debug)]
#[clap(version, author)]
pub struct CliOptions {
    /// Determines the shell behavior, this flag should always be set automatically
    /// by the shell hook. Valid options are: zsh, fish, bash
    #[clap(long)]
    pub shell: ShellName,

    /// Supplied during autoload sequence. Not generally useful to end-users.
    #[clap(long)]
    pub autoload: bool,

    /// For typical commands such as `sauce` and `sauce clear` this outputs the exact
    /// shell output that would have executed. For mutating commands like `sauce config`
    /// and `sauce set`, the change is printed but not saved.
    #[clap(long)]
    pub show: bool,

    /// Valid options: always, never, auto. Auto (default) will attempt to autodetect
    /// whether it should output color based on the existence of a tty.
    #[clap(long, default_value = "auto")]
    pub color: ColorStrategy,

    /// Enables verbose output. This causes all stdout to be mirrored to stderr.
    #[clap(short, long)]
    pub verbose: bool,

    /// Disables normal messaging output after a command is executed.
    #[clap(short, long)]
    pub quiet: bool,

    /// The path which should be sauce'd. Defaults to the current directory.
    #[clap(short, long)]
    pub path: Option<PathBuf>,

    /// Sets a specific saucefile to load, overriding the default lookup mechanisms and
    /// cascading behavior
    #[clap(long)]
    pub file: Option<PathBuf>,

    /// Runs the given command "as" the given "as" namespace.
    #[clap(short, long)]
    pub r#as: Option<String>,

    /// Filters the set of values to load, allowing globs. By default filters apply to
    /// all targets, but also can use the form "<target>:<glob>" to be more specific.
    #[clap(short, long)]
    pub glob: Option<String>,

    /// Only use values for a specific target. Essentially this can be thought of
    /// as a shortcut for `-g '<target>:*`.
    #[clap(short, long)]
    pub target: Option<String>,

    /// Filters the set of values to load, literally. By default filters apply to all
    /// targets, but also can use the form "<target>:<filter>" to be more specific.
    #[clap(short, long)]
    pub filter: Option<String>,

    #[clap(subcommand)]
    pub subcmd: Option<SubCommand>,
}

impl CliOptions {
    pub fn parse() -> Self {
        CliOptions::try_parse().unwrap_or_else(|e| {
            let stderr = std::io::stderr();
            let mut handle = stderr.lock();

            let message = format!("{}", e);
            handle.write_all(message.as_ref()).unwrap();
            handle.flush().unwrap();
            std::process::exit(1)
        })
    }
}

#[derive(Parser, Debug)]
pub enum SubCommand {
    /// Clears the shell of values sauce tracks
    Clear,

    /// Sets local/global configuration options
    Config(ConfigCommand),

    /// Opens the saucefile with your $EDITOR
    Edit,

    /// Moves the targeted saucefile to the location given by `destination`.
    Move(MoveCommand),

    /// Creates a new saucefile for the targeted location
    New,

    /// Sets target values for the targeted location
    Set(SetCommand),

    /// Group of shell related subcommands
    Shell(ShellCommand),

    /// Display the given category of key-value pairs
    Show(ShowCommand),
}

#[derive(Parser, Debug)]
pub struct ConfigCommand {
    #[clap(long, short)]
    pub global: bool,

    #[clap(parse(try_from_str = crate::cli::utilities::parse_key_val))]
    pub values: Vec<(String, String)>,
}

#[derive(Parser, Debug)]
pub struct MoveCommand {
    /// The destination location to which a `sauce` invocation would point.
    /// That is, not the destination saucefile location.
    #[clap()]
    pub destination: PathBuf,

    /// Instead of removing the files at the source location, leave the original
    /// file untouched.
    #[clap(short, long)]
    pub copy: bool,
}

#[derive(Parser, Debug)]
pub struct SetCommand {
    #[clap(subcommand)]
    pub kind: SetKinds,
}

#[derive(Parser, Debug)]
pub enum SetKinds {
    Env(SetVarKind),
    Alias(SetVarKind),
    Function(KeyValuePair),
    File(KeyValuePair),
}

/// Key-value pairs, delimited by an "=".
#[derive(Parser, Debug)]
pub struct SetVarKind {
    #[clap(parse(try_from_str = crate::cli::utilities::parse_key_val))]
    pub values: Vec<(String, String)>,
}

/// Key value pair, supplied as individual arguments
#[derive(Parser, Debug)]
pub struct KeyValuePair {
    pub key: String,
    pub value: String,
}

#[derive(Parser, Debug)]
pub struct ShellCommand {
    #[clap(subcommand)]
    pub kind: ShellKinds,
}

#[derive(Parser, Debug)]
pub enum ShellKinds {
    /// The intialization shell hook for getting sauce functionality
    Init,

    /// Executes a command inside a subshell which has had `sauce` invoked already
    Exec(ExecCommand),
}

#[derive(Parser, Debug)]
pub struct ExecCommand {
    #[clap()]
    pub command: String,
}

#[derive(Parser, Debug)]
pub struct ShowCommand {
    #[clap(subcommand)]
    pub kind: ShowKinds,
}

#[derive(Parser, Debug)]
pub enum ShowKinds {
    Env,
    Alias,
    Function,
    File,
}