Skip to content

Commit 24635d5

Browse files
author
Oleg Sucharevich
authored
add commands to create git based contexts (#190)
1 parent d301643 commit 24635d5

File tree

7 files changed

+365
-2
lines changed

7 files changed

+365
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const debug = require('debug')('codefresh:cli:create:context:git');
2+
const Command = require('../../../../Command');
3+
const createContext = require('../../create.cmd');
4+
5+
const CODEFRESH_GIT_CLONE_REFERENCE = 'https://codefresh.io/docs/docs/codefresh-yaml/steps/git-clone/';
6+
7+
const usage = `Git context are used to clone repositories during pipeline execution.\nLearn more about git context here: ${CODEFRESH_GIT_CLONE_REFERENCE}`;
8+
9+
const command = new Command({
10+
command: 'git',
11+
parent: createContext,
12+
description: 'Create a git context [type]',
13+
usage,
14+
webDocs: {
15+
category: 'Create Context',
16+
subCategory : 'Git',
17+
title: 'Create Git Context',
18+
},
19+
builder: (yargs) => {
20+
return yargs;
21+
},
22+
handler: async (argv) => {
23+
yargs.showHelp();
24+
},
25+
});
26+
27+
module.exports = command;
28+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const debug = require('debug')('codefresh:cli:create:context:git:bitbucket');
2+
const Command = require('../../../../../Command');
3+
const CFError = require('cf-errors');
4+
const createGitCmd = require('./../base.cmd');
5+
const {
6+
context,
7+
} = require('../../../../../../../logic/index').api;
8+
9+
const LINK = 'https://bitbucket.org/account/user/{YOUR-USERNAME}/app-passwords';
10+
11+
12+
const command = new Command({
13+
command: 'bitbucket <name>',
14+
parent: createGitCmd,
15+
description: 'Create a bitbucket context',
16+
usage: `${createGitCmd.usage}\nTo create bitbucket context you need to generate application password from here: ${LINK}`,
17+
webDocs: {
18+
category: 'Create Git Context',
19+
subCategory: 'bitbucket',
20+
title: 'bitbucket',
21+
weight: 10,
22+
},
23+
builder: (yargs) => {
24+
yargs
25+
.option('app-password', {
26+
describe: 'Application password generated in bitbucket',
27+
alias: 'a',
28+
required: true,
29+
})
30+
.option('username', {
31+
describe: 'username that has permissions to use app password',
32+
alias: 'u',
33+
required: true,
34+
})
35+
.option('owner', {
36+
describe: 'Owner of the context, user owned contexts cannot be accessible by other users across the account',
37+
choices: ['account', 'user'],
38+
alias: 'o',
39+
default: 'account',
40+
required: true,
41+
});
42+
return yargs;
43+
},
44+
handler: async (argv) => {
45+
const data = {
46+
apiVersion: 'v1',
47+
kind: 'context',
48+
owner: argv.owner || 'account',
49+
metadata: {
50+
name: argv.name,
51+
},
52+
spec: {
53+
type: 'git.bitbucket',
54+
data: {
55+
host: 'bitbucket.org',
56+
useSSL: true,
57+
auth: {
58+
type: 'basic',
59+
username: argv.username,
60+
password: argv.appPassword,
61+
},
62+
},
63+
},
64+
};
65+
66+
67+
if (!data.metadata.name || !data.spec.type) {
68+
throw new CFError('Name and type must be provided');
69+
}
70+
71+
await context.createContext(data);
72+
console.log(`Context: ${data.metadata.name} created`);
73+
},
74+
});
75+
76+
module.exports = command;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const debug = require('debug')('codefresh:cli:create:context:git:github');
2+
const Command = require('../../../../../Command');
3+
const CFError = require('cf-errors');
4+
const createGitCmd = require('./../base.cmd');
5+
const {
6+
context,
7+
} = require('../../../../../../../logic/index').api;
8+
9+
const LINK = 'https://github.com/settings/tokens';
10+
11+
12+
const command = new Command({
13+
command: 'github <name>',
14+
parent: createGitCmd,
15+
description: 'Create a github context',
16+
usage: `${createGitCmd.usage}\nTo create Github context you need to generate the token here: ${LINK}`,
17+
webDocs: {
18+
category: 'Create Git Context',
19+
subCategory: 'github',
20+
title: 'github',
21+
weight: 10,
22+
},
23+
builder: (yargs) => {
24+
yargs
25+
.option('access-token', {
26+
describe: 'Access token from to be used to clone repositories',
27+
alias: 't',
28+
required: true,
29+
})
30+
.option('owner', {
31+
describe: 'Owner of the context, user owned contexts cannot be accessible by other users across the account',
32+
choices: ['account', 'user'],
33+
alias: 'o',
34+
default: 'account',
35+
required: true,
36+
})
37+
.option('use-ssl', {
38+
describe: 'Connect to the git host using ssl',
39+
choices: [true, false],
40+
type: 'boolean',
41+
alias: 's',
42+
default: true,
43+
required: true,
44+
})
45+
.option('host', {
46+
describe: 'Host name of your github (without protocol)',
47+
alias: 'h',
48+
default: 'github.com',
49+
required: true,
50+
});
51+
return yargs;
52+
},
53+
handler: async (argv) => {
54+
const data = {
55+
apiVersion: 'v1',
56+
kind: 'context',
57+
owner: argv.owner || 'account',
58+
metadata: {
59+
name: argv.name,
60+
},
61+
spec: {
62+
type: 'git.github',
63+
data: {
64+
host: argv.host || 'github.com',
65+
useSSL: Boolean(argv.useSSL) || true,
66+
auth: {
67+
type: 'basic',
68+
username: 'x-oauth-basic',
69+
password: argv.accessToken,
70+
},
71+
},
72+
},
73+
};
74+
75+
76+
if (!data.metadata.name || !data.spec.type) {
77+
throw new CFError('Name and type must be provided');
78+
}
79+
80+
await context.createContext(data);
81+
console.log(`Context: ${data.metadata.name} created`);
82+
},
83+
});
84+
85+
module.exports = command;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const debug = require('debug')('codefresh:cli:create:context:git:gitlab');
2+
const Command = require('../../../../../Command');
3+
const CFError = require('cf-errors');
4+
const createGitCmd = require('./../base.cmd');
5+
const {
6+
context,
7+
} = require('../../../../../../../logic/index').api;
8+
9+
const LINK = 'https://gitlab.com/profile/personal_access_tokens';
10+
11+
12+
const command = new Command({
13+
command: 'gitlab <name>',
14+
parent: createGitCmd,
15+
description: 'Create a gitlab context',
16+
usage: `${createGitCmd.usage}\nTo create gitlab context you need to generate the token here: ${LINK}`,
17+
webDocs: {
18+
category: 'Create Git Context',
19+
subCategory: 'gitlab',
20+
title: 'gitlab',
21+
weight: 10,
22+
},
23+
builder: (yargs) => {
24+
yargs
25+
.option('access-token', {
26+
describe: 'Access token from to be used to clone repositories',
27+
alias: 't',
28+
required: true,
29+
})
30+
.option('owner', {
31+
describe: 'Owner of the context, user owned contexts cannot be accessible by other users across the account',
32+
choices: ['account', 'user'],
33+
alias: 'o',
34+
default: 'account',
35+
required: true,
36+
})
37+
.option('use-ssl', {
38+
describe: 'Connect to the git host using ssl',
39+
choices: [true, false],
40+
type: 'boolean',
41+
alias: 's',
42+
default: true,
43+
required: true,
44+
})
45+
.option('host', {
46+
describe: 'Host name of your gitlab (without protocol)',
47+
alias: 'h',
48+
default: 'gitlab.com',
49+
required: true,
50+
});
51+
return yargs;
52+
},
53+
handler: async (argv) => {
54+
const data = {
55+
apiVersion: 'v1',
56+
kind: 'context',
57+
owner: argv.owner || 'account',
58+
metadata: {
59+
name: argv.name,
60+
},
61+
spec: {
62+
type: 'git.gitlab',
63+
data: {
64+
host: argv.host || 'gitlab.com',
65+
useSSL: Boolean(argv.useSSL) || true,
66+
auth: {
67+
type: 'basic',
68+
username: 'oauth2',
69+
password: argv.accessToken,
70+
},
71+
},
72+
},
73+
};
74+
75+
76+
if (!data.metadata.name || !data.spec.type) {
77+
throw new CFError('Name and type must be provided');
78+
}
79+
80+
await context.createContext(data);
81+
console.log(`Context: ${data.metadata.name} created`);
82+
},
83+
});
84+
85+
module.exports = command;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const debug = require('debug')('codefresh:cli:create:context:git:stash');
2+
const Command = require('../../../../../Command');
3+
const CFError = require('cf-errors');
4+
const createGitCmd = require('./../base.cmd');
5+
const {
6+
context,
7+
} = require('../../../../../../../logic/index').api;
8+
9+
10+
11+
const command = new Command({
12+
command: 'stash <name>',
13+
parent: createGitCmd,
14+
description: 'Create a stash context',
15+
usage: `${createGitCmd.usage}\nTo create Stash context you need to provider username and password`,
16+
webDocs: {
17+
category: 'Create Git Context',
18+
subCategory: 'stash',
19+
title: 'stash',
20+
weight: 10,
21+
},
22+
builder: (yargs) => {
23+
yargs
24+
.option('username', {
25+
describe: 'Username',
26+
alias: 'u',
27+
required: true,
28+
})
29+
.option('password', {
30+
describe: 'Password',
31+
alias: 'p',
32+
required: true,
33+
})
34+
.option('host', {
35+
describe: 'Host name of your stash (without protocol, may include port)',
36+
alias: 'h',
37+
required: true,
38+
})
39+
.option('use-ssl', {
40+
describe: 'Connect to the git host using ssl',
41+
choices: [true, false],
42+
type: 'boolean',
43+
alias: 's',
44+
default: true,
45+
required: true,
46+
})
47+
.option('owner', {
48+
describe: 'Owner of the context, user owned contexts cannot be accessible by other users across the account',
49+
choices: ['account', 'user'],
50+
alias: 'o',
51+
default: 'account',
52+
required: true,
53+
});
54+
return yargs;
55+
},
56+
handler: async (argv) => {
57+
const data = {
58+
apiVersion: 'v1',
59+
kind: 'context',
60+
owner: argv.owner || 'account',
61+
metadata: {
62+
name: argv.name,
63+
},
64+
spec: {
65+
type: 'git.stash',
66+
data: {
67+
host: argv.host || 'stash.com',
68+
useSSL: Boolean(argv.useSSL) || true,
69+
auth: {
70+
type: 'basic',
71+
username: argv.username,
72+
password: argv.password,
73+
},
74+
},
75+
},
76+
};
77+
78+
79+
if (!data.metadata.name || !data.spec.type) {
80+
throw new CFError('Name and type must be provided');
81+
}
82+
83+
await context.createContext(data);
84+
console.log(`Context: ${data.metadata.name} created`);
85+
},
86+
});
87+
88+
module.exports = command;

lib/interface/cli/commands/context/get.cmd.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const command = new Command({
2525
})
2626
.option('type', {
2727
describe: 'Context type',
28-
choices: ['config', 'secret', 'helm-repository', 'yaml', 'secret-yaml'],
28+
choices: ['config', 'secret', 'helm-repository', 'yaml', 'secret-yaml', 'git'],
2929
})
3030
.option('owner', {
3131
describe: 'Owner of the context',
@@ -34,6 +34,7 @@ const command = new Command({
3434
.example('codefresh get context NAME', 'Get context NAME')
3535
.example('codefresh get contexts', 'Get all contexts')
3636
.example('codefresh get context --type secret', 'Get all secret contexts')
37+
.example('codefresh get context --type git.github', 'Get all git based contexts for github kind')
3738
.example('codefresh get context --type helm-repository', 'Get all helm-repository contexts');
3839
},
3940
handler: async (argv) => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.8.52",
3+
"version": "0.8.53",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)