Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Primary navigation
Search or go to…
Project
K
kcfg
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Privacy statement
Keyboard shortcuts
?
What's new
6
Snippets
Groups
Projects
Show more breadcrumbs
Qonfucius
kcfg
Commits
e83c4258
Commit
e83c4258
authored
3 years ago
by
Félix Lescaudey de Maneville
Browse files
Options
Downloads
Patches
Plain Diff
Fixed kcfg init command
parent
42255388
No related branches found
No related tags found
1 merge request
!8
Fixed kcfg init command
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
CHANGELOG.md
+4
-0
4 additions, 0 deletions
CHANGELOG.md
src/app.rs
+1
-1
1 addition, 1 deletion
src/app.rs
src/commands/command_init.rs
+36
-51
36 additions, 51 deletions
src/commands/command_init.rs
src/error.rs
+0
-2
0 additions, 2 deletions
src/error.rs
with
41 additions
and
54 deletions
CHANGELOG.md
+
4
−
0
View file @
e83c4258
# CHANGELOG
## Unreleased
*
Fixed
`init`
command
## 0.1.1
*
Bumped
`clap`
version
...
...
This diff is collapsed.
Click to expand it.
src/app.rs
+
1
−
1
View file @
e83c4258
...
...
@@ -18,7 +18,7 @@ pub struct KcfgApp {
#[derive(Debug,
ArgEnum,
Parser)]
pub
enum
Command
{
///
Initializes the environment relative to your current path
///
Prints the kcfg shell initialization code
Init
(
command_init
::
InitOptions
),
/// Find and use the correct KUBECONFIG
Use
(
command_use
::
UseOptions
),
...
...
This diff is collapsed.
Click to expand it.
src/commands/command_init.rs
+
36
−
51
View file @
e83c4258
use
crate
::
common
::
check_
directory
_path
;
use
crate
::
common
::
check_
file
_path
;
use
crate
::
error
::
KcfgError
;
use
clap
::{
ArgEnum
,
Parser
,
ValueHint
};
use
indoc
::
formatdoc
;
use
std
::
str
::
FromStr
;
/// Options for the `init` command
#[derive(Parser,
Debug)]
pub
struct
InitOptions
{
#[clap(arg_enum
,
default_value
=
"common"
)]
#[clap(arg_enum)]
/// target shell type
pub
shell_type
:
InitShellType
,
pub
shell_type
:
Option
<
InitShellType
>
,
#[clap(short
=
'p'
,
long
=
"path"
,
value_hint
=
ValueHint::DirPath)]
/// Define a custom path
. O
therwise the current
path
will be used
/// Define a custom path
to kcfg program, o
therwise the current
filesystem path to kcfg
will be used
pub
custom_path
:
Option
<
String
>
,
}
...
...
@@ -25,20 +24,6 @@ pub struct InitOptions {
pub
enum
InitShellType
{
Zsh
,
Bash
,
Common
,
}
impl
FromStr
for
InitShellType
{
type
Err
=
KcfgError
;
fn
from_str
(
s
:
&
str
)
->
Result
<
Self
,
Self
::
Err
>
{
match
s
.to_ascii_lowercase
()
.as_str
()
{
"zsh"
=>
Ok
(
InitShellType
::
Zsh
),
"bash"
=>
Ok
(
InitShellType
::
Bash
),
"common"
=>
Ok
(
InitShellType
::
Common
),
_
=>
Err
(
KcfgError
::
NotSupportedShellType
(
s
.to_string
())),
}
}
}
/// Match with the shell type the User has
...
...
@@ -63,15 +48,18 @@ pub fn init(params: InitOptions) -> Result<String, KcfgError> {
.to_string
()
}
Some
(
custom_path
)
=>
{
check_
directory
_path
(
&
custom_path
)
?
;
check_
file
_path
(
&
custom_path
)
?
;
custom_path
}
};
Ok
(
match
params
.shell_type
{
InitShellType
::
Common
=>
init_common_full
(
&
path
),
InitShellType
::
Zsh
=>
init_zsh
(
&
path
),
InitShellType
::
Bash
=>
init_bash
(
&
path
),
})
let
res
=
match
params
.shell_type
{
Some
(
shell_type
)
=>
match
shell_type
{
InitShellType
::
Bash
=>
init_bash
(
&
path
),
InitShellType
::
Zsh
=>
init_zsh
(
&
path
),
},
None
=>
init_common_full
(
&
path
),
};
Ok
(
res
)
}
/// # Arguments
...
...
@@ -83,7 +71,7 @@ pub fn init(params: InitOptions) -> Result<String, KcfgError> {
/// Return a string that contains the shell function to use in the `Common` case
///
fn
init_common_full
(
current_path
:
&
str
)
->
String
{
return
formatdoc!
{
"
formatdoc!
{
"
function kcfg() {{
result=$({cmd} $@)
if [[ $result = 'export '* ]]
...
...
@@ -95,7 +83,7 @@ fn init_common_full(current_path: &str) -> String {
}}
"
,
cmd
=
current_path
}
;
}
}
/// # Arguments
...
...
@@ -107,7 +95,7 @@ fn init_common_full(current_path: &str) -> String {
/// Return a string that contains the shell command to use in the `zsh` case
///
fn
init_zsh
(
current_path
:
&
str
)
->
String
{
format
doc
!
(
"source <({} init
zsh --print-full-init
)"
,
current_path
)
format!
(
"source <({} init)"
,
current_path
)
}
/// # Arguments
...
...
@@ -125,9 +113,9 @@ fn init_bash(current_path: &str) -> String {
local minor=
\"
${{BASH_VERSINFO[1]}}
\"
if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
source <(
\"
{cmd}
\"
init
bash --print-full-init
)
source <(
\"
{cmd}
\"
init)
else
source /dev/stdin <<<
\"
$(
\"
{cmd}
\"
init
bash --print-full-init
)
\"
source /dev/stdin <<<
\"
$(
\"
{cmd}
\"
init)
\"
fi
}}
__main
...
...
@@ -147,7 +135,7 @@ mod tests {
#[test]
fn
can_fail_with_wrong_path
()
{
let
params
=
InitOptions
{
shell_type
:
InitShellType
::
Bash
,
shell_type
:
Some
(
InitShellType
::
Bash
)
,
custom_path
:
Some
(
"toto"
.to_string
()),
};
match
init
(
params
)
{
...
...
@@ -161,15 +149,15 @@ mod tests {
}
#[test]
fn
can_fail_with_wrong_
directory
()
{
fn
can_fail_with_wrong_
filr
()
{
let
params
=
InitOptions
{
shell_type
:
InitShellType
::
Bash
,
custom_path
:
Some
(
"
./Cargo.toml
"
.to_string
()),
shell_type
:
Some
(
InitShellType
::
Bash
)
,
custom_path
:
Some
(
"
src
"
.to_string
()),
};
match
init
(
params
)
{
Ok
(
_
)
=>
panic!
(
"Test should have failed"
),
Err
(
e
)
=>
{
if
!
matches!
(
e
,
KcfgError
::
Wrong
Directory
(
_
))
{
if
!
matches!
(
e
,
KcfgError
::
Wrong
File
(
_
))
{
panic!
(
"Test failed with wrong error"
);
}
}
...
...
@@ -179,8 +167,8 @@ mod tests {
#[test]
fn
can_succeed_with_bash
()
{
let
params
=
InitOptions
{
shell_type
:
InitShellType
::
Bash
,
custom_path
:
Some
(
"
/tmp
"
.to_string
()),
shell_type
:
Some
(
InitShellType
::
Bash
)
,
custom_path
:
Some
(
"
src/main.rs
"
.to_string
()),
};
let
res
=
init
(
params
)
.unwrap
();
assert_eq!
(
...
...
@@ -190,9 +178,9 @@ mod tests {
local minor=
\"
${{BASH_VERSINFO[1]}}
\"
if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
source <(
\"
/tmp
\"
init bash --print-full-
init)
source <(
\"
src/main.rs
\"
init)
else
source /dev/stdin <<<
\"
$(
\"
/tmp
\"
init bash --print-full-
init)
\"
source /dev/stdin <<<
\"
$(
\"
src/main.rs
\"
init)
\"
fi
}}
__main
...
...
@@ -205,24 +193,24 @@ mod tests {
#[test]
fn
can_succeed_with_zsh
()
{
let
params
=
InitOptions
{
shell_type
:
InitShellType
::
Zsh
,
custom_path
:
Some
(
"
/tmp
"
.to_string
()),
shell_type
:
Some
(
InitShellType
::
Zsh
)
,
custom_path
:
Some
(
"
src/main.rs
"
.to_string
()),
};
let
res
=
init
(
params
)
.unwrap
();
assert_eq!
(
"source <(
/tmp init zsh --print-full-
init)"
.to_string
(),
res
,)
assert_eq!
(
"source <(
src/main.rs
init)"
.to_string
(),
res
,)
}
#[test]
fn
can_succeed_with_common
()
{
let
params
=
InitOptions
{
shell_type
:
InitShellType
::
Common
,
custom_path
:
Some
(
"
/tmp
"
.to_string
()),
shell_type
:
None
,
custom_path
:
Some
(
"
src/main.rs
"
.to_string
()),
};
let
res
=
init
(
params
)
.unwrap
();
assert_eq!
(
formatdoc!
{
"
function kcfg() {{
result=$(
/tmp
$@)
result=$(
src/main.rs
$@)
if [[ $result = 'export '* ]]
then
eval $result
...
...
@@ -259,10 +247,7 @@ mod tests {
#[test]
fn
test_init_zsh
()
{
assert_eq!
(
"source <(test init zsh --print-full-init)"
.to_string
(),
init_zsh
(
"test"
)
);
assert_eq!
(
"source <(test init)"
.to_string
(),
init_zsh
(
"test"
));
}
#[test]
...
...
@@ -274,9 +259,9 @@ mod tests {
local minor=
\"
${{BASH_VERSINFO[1]}}
\"
if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
source <(
\"
test
\"
init
bash --print-full-init
)
source <(
\"
test
\"
init)
else
source /dev/stdin <<<
\"
$(
\"
test
\"
init
bash --print-full-init
)
\"
source /dev/stdin <<<
\"
$(
\"
test
\"
init)
\"
fi
}}
__main
...
...
This diff is collapsed.
Click to expand it.
src/error.rs
+
0
−
2
View file @
e83c4258
...
...
@@ -5,8 +5,6 @@ use thiserror::Error;
#[derive(Debug,
Error)]
pub
enum
KcfgError
{
#[error(
"{0} is not a supported shell type"
)]
NotSupportedShellType
(
String
),
#[error(
"{0} is not a valid path"
)]
InvalidPath
(
PathBuf
),
#[error(
"{0} should be a directory"
)]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment