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
246f427b
Commit
246f427b
authored
3 years ago
by
Félix Lescaudey de Maneville
Browse files
Options
Downloads
Plain Diff
Merge branch 'feat/fix_command_init' into 'develop'
Fixed kcfg init command See merge request
!8
parents
42255388
e83c4258
No related branches found
No related tags found
1 merge request
!8
Fixed kcfg init command
Pipeline
#393469352
passed
3 years ago
Stage: build
Stage: test
Stage: build_doc
Changes
4
Pipelines
1
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 @
246f427b
# CHANGELOG
# CHANGELOG
## Unreleased
*
Fixed
`init`
command
## 0.1.1
## 0.1.1
*
Bumped
`clap`
version
*
Bumped
`clap`
version
...
...
This diff is collapsed.
Click to expand it.
src/app.rs
+
1
−
1
View file @
246f427b
...
@@ -18,7 +18,7 @@ pub struct KcfgApp {
...
@@ -18,7 +18,7 @@ pub struct KcfgApp {
#[derive(Debug,
ArgEnum,
Parser)]
#[derive(Debug,
ArgEnum,
Parser)]
pub
enum
Command
{
pub
enum
Command
{
///
Initializes the environment relative to your current path
///
Prints the kcfg shell initialization code
Init
(
command_init
::
InitOptions
),
Init
(
command_init
::
InitOptions
),
/// Find and use the correct KUBECONFIG
/// Find and use the correct KUBECONFIG
Use
(
command_use
::
UseOptions
),
Use
(
command_use
::
UseOptions
),
...
...
This diff is collapsed.
Click to expand it.
src/commands/command_init.rs
+
36
−
51
View file @
246f427b
use
crate
::
common
::
check_
directory
_path
;
use
crate
::
common
::
check_
file
_path
;
use
crate
::
error
::
KcfgError
;
use
crate
::
error
::
KcfgError
;
use
clap
::{
ArgEnum
,
Parser
,
ValueHint
};
use
clap
::{
ArgEnum
,
Parser
,
ValueHint
};
use
indoc
::
formatdoc
;
use
indoc
::
formatdoc
;
use
std
::
str
::
FromStr
;
/// Options for the `init` command
/// Options for the `init` command
#[derive(Parser,
Debug)]
#[derive(Parser,
Debug)]
pub
struct
InitOptions
{
pub
struct
InitOptions
{
#[clap(arg_enum
,
default_value
=
"common"
)]
#[clap(arg_enum)]
/// target shell type
/// target shell type
pub
shell_type
:
InitShellType
,
pub
shell_type
:
Option
<
InitShellType
>
,
#[clap(short
=
'p'
,
long
=
"path"
,
value_hint
=
ValueHint::DirPath)]
#[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
>
,
pub
custom_path
:
Option
<
String
>
,
}
}
...
@@ -25,20 +24,6 @@ pub struct InitOptions {
...
@@ -25,20 +24,6 @@ pub struct InitOptions {
pub
enum
InitShellType
{
pub
enum
InitShellType
{
Zsh
,
Zsh
,
Bash
,
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
/// Match with the shell type the User has
...
@@ -63,15 +48,18 @@ pub fn init(params: InitOptions) -> Result<String, KcfgError> {
...
@@ -63,15 +48,18 @@ pub fn init(params: InitOptions) -> Result<String, KcfgError> {
.to_string
()
.to_string
()
}
}
Some
(
custom_path
)
=>
{
Some
(
custom_path
)
=>
{
check_
directory
_path
(
&
custom_path
)
?
;
check_
file
_path
(
&
custom_path
)
?
;
custom_path
custom_path
}
}
};
};
Ok
(
match
params
.shell_type
{
let
res
=
match
params
.shell_type
{
InitShellType
::
Common
=>
init_common_full
(
&
path
),
Some
(
shell_type
)
=>
match
shell_type
{
InitShellType
::
Zsh
=>
init_zsh
(
&
path
),
InitShellType
::
Bash
=>
init_bash
(
&
path
),
InitShellType
::
Bash
=>
init_bash
(
&
path
),
InitShellType
::
Zsh
=>
init_zsh
(
&
path
),
})
},
None
=>
init_common_full
(
&
path
),
};
Ok
(
res
)
}
}
/// # Arguments
/// # Arguments
...
@@ -83,7 +71,7 @@ pub fn init(params: InitOptions) -> Result<String, KcfgError> {
...
@@ -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
/// Return a string that contains the shell function to use in the `Common` case
///
///
fn
init_common_full
(
current_path
:
&
str
)
->
String
{
fn
init_common_full
(
current_path
:
&
str
)
->
String
{
return
formatdoc!
{
"
formatdoc!
{
"
function kcfg() {{
function kcfg() {{
result=$({cmd} $@)
result=$({cmd} $@)
if [[ $result = 'export '* ]]
if [[ $result = 'export '* ]]
...
@@ -95,7 +83,7 @@ fn init_common_full(current_path: &str) -> String {
...
@@ -95,7 +83,7 @@ fn init_common_full(current_path: &str) -> String {
}}
}}
"
,
"
,
cmd
=
current_path
cmd
=
current_path
}
;
}
}
}
/// # Arguments
/// # Arguments
...
@@ -107,7 +95,7 @@ fn init_common_full(current_path: &str) -> String {
...
@@ -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
/// Return a string that contains the shell command to use in the `zsh` case
///
///
fn
init_zsh
(
current_path
:
&
str
)
->
String
{
fn
init_zsh
(
current_path
:
&
str
)
->
String
{
format
doc
!
(
"source <({} init
zsh --print-full-init
)"
,
current_path
)
format!
(
"source <({} init)"
,
current_path
)
}
}
/// # Arguments
/// # Arguments
...
@@ -125,9 +113,9 @@ fn init_bash(current_path: &str) -> String {
...
@@ -125,9 +113,9 @@ fn init_bash(current_path: &str) -> String {
local minor=
\"
${{BASH_VERSINFO[1]}}
\"
local minor=
\"
${{BASH_VERSINFO[1]}}
\"
if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
source <(
\"
{cmd}
\"
init
bash --print-full-init
)
source <(
\"
{cmd}
\"
init)
else
else
source /dev/stdin <<<
\"
$(
\"
{cmd}
\"
init
bash --print-full-init
)
\"
source /dev/stdin <<<
\"
$(
\"
{cmd}
\"
init)
\"
fi
fi
}}
}}
__main
__main
...
@@ -147,7 +135,7 @@ mod tests {
...
@@ -147,7 +135,7 @@ mod tests {
#[test]
#[test]
fn
can_fail_with_wrong_path
()
{
fn
can_fail_with_wrong_path
()
{
let
params
=
InitOptions
{
let
params
=
InitOptions
{
shell_type
:
InitShellType
::
Bash
,
shell_type
:
Some
(
InitShellType
::
Bash
)
,
custom_path
:
Some
(
"toto"
.to_string
()),
custom_path
:
Some
(
"toto"
.to_string
()),
};
};
match
init
(
params
)
{
match
init
(
params
)
{
...
@@ -161,15 +149,15 @@ mod tests {
...
@@ -161,15 +149,15 @@ mod tests {
}
}
#[test]
#[test]
fn
can_fail_with_wrong_
directory
()
{
fn
can_fail_with_wrong_
filr
()
{
let
params
=
InitOptions
{
let
params
=
InitOptions
{
shell_type
:
InitShellType
::
Bash
,
shell_type
:
Some
(
InitShellType
::
Bash
)
,
custom_path
:
Some
(
"
./Cargo.toml
"
.to_string
()),
custom_path
:
Some
(
"
src
"
.to_string
()),
};
};
match
init
(
params
)
{
match
init
(
params
)
{
Ok
(
_
)
=>
panic!
(
"Test should have failed"
),
Ok
(
_
)
=>
panic!
(
"Test should have failed"
),
Err
(
e
)
=>
{
Err
(
e
)
=>
{
if
!
matches!
(
e
,
KcfgError
::
Wrong
Directory
(
_
))
{
if
!
matches!
(
e
,
KcfgError
::
Wrong
File
(
_
))
{
panic!
(
"Test failed with wrong error"
);
panic!
(
"Test failed with wrong error"
);
}
}
}
}
...
@@ -179,8 +167,8 @@ mod tests {
...
@@ -179,8 +167,8 @@ mod tests {
#[test]
#[test]
fn
can_succeed_with_bash
()
{
fn
can_succeed_with_bash
()
{
let
params
=
InitOptions
{
let
params
=
InitOptions
{
shell_type
:
InitShellType
::
Bash
,
shell_type
:
Some
(
InitShellType
::
Bash
)
,
custom_path
:
Some
(
"
/tmp
"
.to_string
()),
custom_path
:
Some
(
"
src/main.rs
"
.to_string
()),
};
};
let
res
=
init
(
params
)
.unwrap
();
let
res
=
init
(
params
)
.unwrap
();
assert_eq!
(
assert_eq!
(
...
@@ -190,9 +178,9 @@ mod tests {
...
@@ -190,9 +178,9 @@ mod tests {
local minor=
\"
${{BASH_VERSINFO[1]}}
\"
local minor=
\"
${{BASH_VERSINFO[1]}}
\"
if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
source <(
\"
/tmp
\"
init bash --print-full-
init)
source <(
\"
src/main.rs
\"
init)
else
else
source /dev/stdin <<<
\"
$(
\"
/tmp
\"
init bash --print-full-
init)
\"
source /dev/stdin <<<
\"
$(
\"
src/main.rs
\"
init)
\"
fi
fi
}}
}}
__main
__main
...
@@ -205,24 +193,24 @@ mod tests {
...
@@ -205,24 +193,24 @@ mod tests {
#[test]
#[test]
fn
can_succeed_with_zsh
()
{
fn
can_succeed_with_zsh
()
{
let
params
=
InitOptions
{
let
params
=
InitOptions
{
shell_type
:
InitShellType
::
Zsh
,
shell_type
:
Some
(
InitShellType
::
Zsh
)
,
custom_path
:
Some
(
"
/tmp
"
.to_string
()),
custom_path
:
Some
(
"
src/main.rs
"
.to_string
()),
};
};
let
res
=
init
(
params
)
.unwrap
();
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]
#[test]
fn
can_succeed_with_common
()
{
fn
can_succeed_with_common
()
{
let
params
=
InitOptions
{
let
params
=
InitOptions
{
shell_type
:
InitShellType
::
Common
,
shell_type
:
None
,
custom_path
:
Some
(
"
/tmp
"
.to_string
()),
custom_path
:
Some
(
"
src/main.rs
"
.to_string
()),
};
};
let
res
=
init
(
params
)
.unwrap
();
let
res
=
init
(
params
)
.unwrap
();
assert_eq!
(
assert_eq!
(
formatdoc!
{
"
formatdoc!
{
"
function kcfg() {{
function kcfg() {{
result=$(
/tmp
$@)
result=$(
src/main.rs
$@)
if [[ $result = 'export '* ]]
if [[ $result = 'export '* ]]
then
then
eval $result
eval $result
...
@@ -259,10 +247,7 @@ mod tests {
...
@@ -259,10 +247,7 @@ mod tests {
#[test]
#[test]
fn
test_init_zsh
()
{
fn
test_init_zsh
()
{
assert_eq!
(
assert_eq!
(
"source <(test init)"
.to_string
(),
init_zsh
(
"test"
));
"source <(test init zsh --print-full-init)"
.to_string
(),
init_zsh
(
"test"
)
);
}
}
#[test]
#[test]
...
@@ -274,9 +259,9 @@ mod tests {
...
@@ -274,9 +259,9 @@ mod tests {
local minor=
\"
${{BASH_VERSINFO[1]}}
\"
local minor=
\"
${{BASH_VERSINFO[1]}}
\"
if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
source <(
\"
test
\"
init
bash --print-full-init
)
source <(
\"
test
\"
init)
else
else
source /dev/stdin <<<
\"
$(
\"
test
\"
init
bash --print-full-init
)
\"
source /dev/stdin <<<
\"
$(
\"
test
\"
init)
\"
fi
fi
}}
}}
__main
__main
...
...
This diff is collapsed.
Click to expand it.
src/error.rs
+
0
−
2
View file @
246f427b
...
@@ -5,8 +5,6 @@ use thiserror::Error;
...
@@ -5,8 +5,6 @@ use thiserror::Error;
#[derive(Debug,
Error)]
#[derive(Debug,
Error)]
pub
enum
KcfgError
{
pub
enum
KcfgError
{
#[error(
"{0} is not a supported shell type"
)]
NotSupportedShellType
(
String
),
#[error(
"{0} is not a valid path"
)]
#[error(
"{0} is not a valid path"
)]
InvalidPath
(
PathBuf
),
InvalidPath
(
PathBuf
),
#[error(
"{0} should be a directory"
)]
#[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