golem-cli 0.0.120

Command line interface for Golem.
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Copyright 2024 Golem Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::command::profile::{ProfileSubCommand, UniversalProfileAdd};
use crate::config::{CloudProfile, Config, OssProfile, Profile, ProfileConfig, ProfileName};
use crate::examples;
use crate::model::{Format, GolemError, GolemResult};
use crate::stubgen::handle_stubgen;
use async_trait::async_trait;
use clap::{Parser, Subcommand};
use clap_verbosity_flag::Verbosity;
use colored::Colorize;
use golem_examples::model::{ExampleName, GuestLanguage, GuestLanguageTier, PackageName};
use indoc::formatdoc;
use inquire::{Confirm, CustomType, Select};
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use strum::IntoEnumIterator;
use strum_macros::EnumIter;
use url::{ParseError, Url};

#[derive(Subcommand, Debug)]
#[command()]
pub enum InitCommand<ProfileAdd: clap::Args> {
    /// Create a new default profile and switch to it
    #[command()]
    Init {},

    /// Manage profiles
    #[command()]
    Profile {
        #[command(subcommand)]
        subcommand: ProfileSubCommand<ProfileAdd>,
    },

    /// Create a new Golem component from built-in examples
    #[command()]
    New {
        /// Name of the example to use
        #[arg(short, long)]
        example: ExampleName,

        /// The new component's name
        #[arg(short, long)]
        component_name: golem_examples::model::ComponentName,

        /// The package name of the generated component (in namespace:name format)
        #[arg(short, long)]
        package_name: Option<PackageName>,
    },

    /// Lists the built-in examples available for creating new components
    #[command()]
    ListExamples {
        /// The minimum language tier to include in the list
        #[arg(short, long)]
        min_tier: Option<GuestLanguageTier>,

        /// Filter examples by a given guest language
        #[arg(short, long)]
        language: Option<GuestLanguage>,
    },

    /// WASM RPC stub generator
    #[cfg(feature = "stubgen")]
    Stubgen {
        #[command(subcommand)]
        subcommand: golem_wasm_rpc_stubgen::Command,
    },
}

#[derive(Parser, Debug)]
#[command(author, version = option_env ! ("VERSION").unwrap_or(env ! ("CARGO_PKG_VERSION")), about = "Your Golem is not configured. Please run `golem-cli init`", long_about = None, rename_all = "kebab-case")]
/// Your Golem is not configured. Please run `golem-cli init`
pub struct GolemInitCommand<ProfileAdd: clap::Args> {
    #[command(flatten)]
    pub verbosity: Verbosity,

    #[arg(short = 'F', long, global = true, default_value = "text")]
    pub format: Format,

    #[command(subcommand)]
    pub command: InitCommand<ProfileAdd>,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum CliKind {
    Universal,
    Golem,
    Cloud,
}

#[async_trait]
pub trait ProfileAuth {
    async fn auth(&self, profile_name: &ProfileName, config_dir: &Path) -> Result<(), GolemError>;
}

pub struct DummyProfileAuth {}

#[async_trait]
impl ProfileAuth for DummyProfileAuth {
    async fn auth(
        &self,
        _profile_name: &ProfileName,
        _config_dir: &Path,
    ) -> Result<(), GolemError> {
        Ok(())
    }
}

pub async fn async_main<ProfileAdd: Into<UniversalProfileAdd> + clap::Args>(
    cmd: GolemInitCommand<ProfileAdd>,
    cli_kind: CliKind,
    config_dir: PathBuf,
    profile_auth: Box<dyn ProfileAuth + Send + Sync + 'static>,
) -> Result<(), Box<dyn std::error::Error>> {
    let res = match cmd.command {
        InitCommand::Init {} => {
            let profile_name = ProfileName::default(cli_kind);

            let res = init_profile(cli_kind, profile_name, &config_dir).await?;

            if res.auth_required {
                profile_auth.auth(&res.profile_name, &config_dir).await?
            }

            Ok(GolemResult::Str("Profile created".to_string()))
        }
        InitCommand::Profile { subcommand } => {
            subcommand
                .handle(cli_kind, &config_dir, profile_auth.as_ref())
                .await
        }
        InitCommand::New {
            example,
            package_name,
            component_name,
        } => examples::process_new(example, component_name, package_name),
        InitCommand::ListExamples { min_tier, language } => {
            examples::process_list_examples(min_tier, language)
        }
        #[cfg(feature = "stubgen")]
        InitCommand::Stubgen { subcommand } => handle_stubgen(subcommand).await,
    };

    match res {
        Ok(res) => {
            res.print(cmd.format);
            Ok(())
        }
        Err(err) => Err(Box::new(err)),
    }
}

fn validate_profile_override(
    profile_name: &ProfileName,
    config_dir: &Path,
) -> Result<(), GolemError> {
    if Config::get_profile(profile_name, config_dir).is_some() {
        let question = formatdoc!(
            "Profile '{}' already exists.
            Do you want to override it?",
            profile_name.to_string().bold()
        );

        let ans = Confirm::new(&question)
            .with_default(false)
            .prompt()
            .map_err(|err| GolemError(format!("Unexpected error: {err}")))?;

        if ans {
            Ok(())
        } else {
            Err(GolemError("Profile creation was interrupted.".to_string()))
        }
    } else {
        Ok(())
    }
}

#[derive(Debug, Clone, Copy, EnumIter)]
enum ProfileType {
    Golem,
    GolemCloud,
}

impl Display for ProfileType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ProfileType::Golem => write!(f, "{}. For stand-alone installations", "Golem".bold()),
            ProfileType::GolemCloud => write!(
                f,
                "{}. To use cloud version provided by https://golem.cloud/",
                "Golem Cloud".bold()
            ),
        }
    }
}

fn select_type() -> Result<ProfileType, GolemError> {
    let options = ProfileType::iter().collect::<Vec<_>>();
    Select::new("Select profile type:", options)
        .prompt()
        .map_err(|err| GolemError(format!("Unexpected error: {err}")))
}

#[derive(Debug, Copy, Clone, EnumIter)]
enum InitFormat {
    Text,
    Yaml,
    Json,
}

impl From<InitFormat> for Format {
    fn from(value: InitFormat) -> Self {
        match value {
            InitFormat::Text => Format::Text,
            InitFormat::Yaml => Format::Yaml,
            InitFormat::Json => Format::Json,
        }
    }
}

impl Display for InitFormat {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let name = match self {
            InitFormat::Text => "text",
            InitFormat::Yaml => "yaml",
            InitFormat::Json => "json",
        };

        write!(f, "{}", name)
    }
}

impl FromStr for InitFormat {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "json" => Ok(InitFormat::Json),
            "yaml" => Ok(InitFormat::Yaml),
            "text" => Ok(InitFormat::Text),
            _ => {
                let all = InitFormat::iter()
                    .map(|x| format!("\"{x}\""))
                    .collect::<Vec<String>>()
                    .join(", ");
                Err(format!("Unknown format: {s}. Expected one of {all}"))
            }
        }
    }
}

fn make_profile_config() -> Result<ProfileConfig, GolemError> {
    let options = InitFormat::iter().collect::<Vec<_>>();
    let default_format = Select::new("Default output format:", options)
        .prompt()
        .map_err(|err| GolemError(format!("Unexpected error: {err}")))?
        .into();

    Ok(ProfileConfig { default_format })
}

fn make_cloud_profile() -> Result<Profile, GolemError> {
    let mut profile = CloudProfile::default();

    let config = make_profile_config()?;

    profile.config = config;

    Ok(Profile::GolemCloud(profile))
}

fn set_active_profile(
    cli_kind: CliKind,
    profile_name: &ProfileName,
    config_dir: &Path,
) -> Result<(), GolemError> {
    let active_profile = Config::get_active_profile(cli_kind, config_dir).map(|p| p.name);

    match active_profile {
        None => Config::set_active_profile_name(profile_name.clone(), cli_kind, config_dir),
        Some(active_profile) if &active_profile == profile_name => Ok(()),
        Some(active_profile) => {
            let question = formatdoc!(
                "
                Current active profile is '{active_profile}'.
                Do you want to switch active profile to '{profile_name}'?
                "
            );

            let ans = Confirm::new(&question)
                .with_default(true)
                .prompt()
                .map_err(|err| GolemError(format!("Unexpected error: {err}")))?;

            if ans {
                Config::set_active_profile_name(profile_name.clone(), cli_kind, config_dir)
            } else {
                Ok(())
            }
        }
    }
}

async fn ask_auth_cloud() -> Result<bool, GolemError> {
    let res = Confirm::new("Do you want to log in to Golem Cloud right now?")
        .with_default(false)
        .with_help_message("You can safely skip this and log in to Golem Cloud later by calling any command that requires authentication.")
        .prompt()
        .map_err(|err| GolemError(format!("Unexpected error: {err}")))?;

    Ok(res)
}

fn ask_for_component_url() -> Result<Url, GolemError> {
    CustomType::<Url>::new("Component service URL:")
        .with_error_message("Please type a valid URL. For instance: http://localhost:9876")
        .prompt()
        .map_err(|err| GolemError(format!("Unexpected error: {err}")))
}

#[derive(Debug, Clone)]
struct OptionalUrl(Option<Url>);

impl Display for OptionalUrl {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match &self.0 {
            None => Ok(()),
            Some(url) => write!(f, "{}", url),
        }
    }
}

impl FromStr for OptionalUrl {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.trim().is_empty() {
            Ok(OptionalUrl(None))
        } else {
            Ok(OptionalUrl(Some(Url::from_str(s)?)))
        }
    }
}

fn ask_for_worker_url() -> Result<Option<Url>, GolemError> {
    CustomType::<OptionalUrl>::new("Worker service URL (empty to use component service url):")
        .with_error_message("Please type a valid URL. For instance: http://localhost:9876")
        .prompt()
        .map(|o| o.0)
        .map_err(|err| GolemError(format!("Unexpected error: {err}")))
}

fn make_oss_profile() -> Result<Profile, GolemError> {
    let url = ask_for_component_url()?;
    let worker_url = ask_for_worker_url()?;

    let help = formatdoc!(
        "Disables certificate validation.
        Warning! Any certificate will be trusted for use. This includes expired certificates.
        This introduces significant vulnerabilities, and should only be used as a last resort."
    );

    let allow_insecure = Confirm::new("Accept invalid certificates?")
        .with_default(false)
        .with_help_message(&help)
        .prompt()
        .map_err(|err| GolemError(format!("Unexpected error: {err}")))?;

    let config = make_profile_config()?;

    Ok(Profile::Golem(OssProfile {
        url,
        worker_url,
        allow_insecure,
        config,
    }))
}

pub struct InitResult {
    pub profile_name: ProfileName,
    pub auth_required: bool,
}

pub async fn init_profile(
    cli_kind: CliKind,
    profile_name: ProfileName,
    config_dir: &Path,
) -> Result<InitResult, GolemError> {
    validate_profile_override(&profile_name, config_dir)?;
    let typ = match cli_kind {
        CliKind::Universal => select_type()?,
        CliKind::Golem => ProfileType::Golem,
        CliKind::Cloud => ProfileType::GolemCloud,
    };

    let profile = match typ {
        ProfileType::Golem => make_oss_profile()?,
        ProfileType::GolemCloud => make_cloud_profile()?,
    };

    Config::set_profile(profile_name.clone(), profile, config_dir)?;

    set_active_profile(cli_kind, &profile_name, config_dir)?;

    let auth_required = if let ProfileType::GolemCloud = typ {
        ask_auth_cloud().await?
    } else {
        false
    };

    Ok(InitResult {
        profile_name,
        auth_required,
    })
}