Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 41d5314

Browse files
feat(projects): implement all project commands (#6)
* feat(projects): implement CreateProject function * refactor(projects): change how config and requests are handled * fix(projects): fix test using new response format * feat(projects): add delete command * feat(projects): add checks for auth and init before commands * feat(projects): implement deploy command * refactor(projects): remove useless commands * feat(projects): implement paastech down command * feat(projects): implement logs command * fix(utils): make message type agnostic in errors (string array or string) * feat(projects): add status command * feat(projects): add stats to status command * fix(projects): add check for git repo on init command * fix(projects): force env var keys to upper case * feat(projects): add remote on init * feat(projects): add push command * fix(projects): change default builder for paketo
1 parent d3f59f5 commit 41d5314

24 files changed

Lines changed: 791 additions & 244 deletions

cmd/account.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@ import (
66

77
"github.com/paastech-cloud/cli/internal/config"
88
"github.com/spf13/cobra"
9-
"github.com/spf13/viper"
109
)
1110

1211
var accountCmd = &cobra.Command{
1312
GroupID: "account",
1413
Use: "account",
1514
Short: "Get infos about user account",
1615
RunE: func(cmd *cobra.Command, args []string) error {
17-
jwt, err := config.ExtractJWTInfos()
16+
userCfg, err := config.LoadAuthConfig()
17+
if err != nil {
18+
return err
19+
}
20+
21+
jwt, err := config.ExtractJWTInfos(userCfg)
1822
if err != nil {
1923
return err
2024
}
2125

2226
fmt.Println("👤 You are logged in as: " + jwt.Username)
23-
fmt.Println("🌐 Server: " + viper.GetString("server"))
27+
fmt.Println("🌐 Server: " + userCfg.GetString("server"))
2428
timeDiff := jwt.ExpirationTime.Sub(time.Now())
2529
if timeDiff > 0 {
2630
fmt.Println(

cmd/delete.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package cmd
22

33
import (
44
"bufio"
5-
"errors"
65
"fmt"
76
"os"
87

9-
"github.com/go-git/go-git/v5"
8+
"github.com/paastech-cloud/cli/internal/config"
109
"github.com/paastech-cloud/cli/pkg/project"
1110
"github.com/paastech-cloud/cli/pkg/utils"
1211
"github.com/spf13/cobra"
@@ -19,16 +18,34 @@ var deleteCmd = &cobra.Command{
1918
RunE: func(cmd *cobra.Command, args []string) error {
2019
fmt.Println("Deleting this project from PaasTech")
2120

22-
// check if git repo exists
23-
git, err := git.PlainOpen(".")
21+
userCfg, err := config.LoadAuthConfig()
2422
if err != nil {
25-
return errors.New("no git repository found in current directory")
23+
return err
24+
}
25+
26+
projCfg, err := config.LoadProjectConfig()
27+
if err != nil {
28+
return err
2629
}
2730

2831
// confirmation
2932
stdin := bufio.NewReader(os.Stdin)
3033
if utils.ConfirmationPrompt(stdin) {
31-
return project.DeleteProject(git)
34+
var project project.Project
35+
projCfg.UnmarshalKey("project", &project)
36+
37+
err := project.Delete(userCfg.GetString("server"), userCfg.GetString("jwt"))
38+
if err != nil {
39+
return err
40+
}
41+
42+
os.Remove("paastech.yaml")
43+
if err != nil {
44+
return err
45+
}
46+
47+
fmt.Println("Project deleted successfully")
48+
return nil
3249
}
3350

3451
return nil

cmd/deploy.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package cmd
22

33
import (
4+
"errors"
45
"fmt"
56

6-
"github.com/paastech-cloud/cli/pkg/deployment"
7+
"github.com/paastech-cloud/cli/internal/config"
8+
"github.com/paastech-cloud/cli/pkg/project"
79
"github.com/spf13/cobra"
810
)
911

@@ -12,8 +14,37 @@ var deployCmd = &cobra.Command{
1214
Use: "deploy",
1315
Short: "Deploy a project",
1416
RunE: func(cmd *cobra.Command, args []string) error {
15-
fmt.Print("Deploying project")
16-
return deployment.Deploy()
17+
fmt.Println("Deploying project to PaaSTech")
18+
19+
userCfg, err := config.LoadAuthConfig()
20+
if err != nil {
21+
return err
22+
}
23+
24+
connected, err := config.IsAuthenticated(userCfg)
25+
if err != nil {
26+
return err
27+
}
28+
if !connected {
29+
return errors.New("Not logged in")
30+
}
31+
32+
projCfg, err := config.LoadProjectConfig()
33+
if err != nil {
34+
return err
35+
}
36+
37+
var project project.Project
38+
projCfg.UnmarshalKey("project", &project)
39+
40+
err = project.Deploy(userCfg.GetString("server"), userCfg.GetString("jwt"), projCfg.GetStringMapString("env"))
41+
if err != nil {
42+
return err
43+
}
44+
45+
fmt.Println("Project successfully deployed")
46+
47+
return nil
1748
},
1849
}
1950

cmd/destroy.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

cmd/down.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/paastech-cloud/cli/internal/config"
8+
"github.com/paastech-cloud/cli/pkg/project"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
var downCmd = &cobra.Command{
13+
GroupID: "deployment",
14+
Use: "down",
15+
Short: "Stop a deployment",
16+
RunE: func(cmd *cobra.Command, args []string) error {
17+
fmt.Println("Stopping deployment")
18+
19+
userCfg, err := config.LoadAuthConfig()
20+
if err != nil {
21+
return err
22+
}
23+
24+
connected, err := config.IsAuthenticated(userCfg)
25+
if err != nil {
26+
return err
27+
}
28+
if !connected {
29+
return errors.New("Not logged in")
30+
}
31+
32+
projCfg, err := config.LoadProjectConfig()
33+
if err != nil {
34+
return err
35+
}
36+
37+
var project project.Project
38+
projCfg.UnmarshalKey("project", &project)
39+
40+
err = project.Down(userCfg.GetString("server"), userCfg.GetString("jwt"))
41+
if err != nil {
42+
return err
43+
}
44+
45+
fmt.Println("Project successfully stopped")
46+
47+
return nil
48+
},
49+
}
50+
51+
func init() {
52+
rootCmd.AddCommand(downCmd)
53+
}

cmd/env.go

Lines changed: 0 additions & 59 deletions
This file was deleted.

cmd/init.go

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,79 @@ import (
55
"fmt"
66

77
"github.com/go-git/go-git/v5"
8+
gitconf "github.com/go-git/go-git/v5/config"
9+
"github.com/paastech-cloud/cli/internal/config"
810
"github.com/paastech-cloud/cli/pkg/project"
911
"github.com/spf13/cobra"
1012
)
1113

1214
var initCmd = &cobra.Command{
1315
GroupID: "project",
14-
Use: "init",
16+
Use: "init [Project Name]",
1517
Short: "Initialize a project",
18+
Args: cobra.ExactArgs(1),
1619
RunE: func(cmd *cobra.Command, args []string) error {
1720
fmt.Println("Initializing a new project")
1821

19-
git, err := git.PlainOpen(".")
22+
// Check if current directory is a git repo
23+
repo, err := git.PlainOpen(".")
2024
if err != nil {
21-
return errors.New("no git repository found in current directory")
25+
return errors.New("No git repository found in current directory")
2226
}
2327

24-
return project.InitProject(git)
28+
// Load User config
29+
userCfg, err := config.LoadAuthConfig()
30+
if err != nil {
31+
return err
32+
}
33+
34+
// Check if user is authenticated
35+
connected, err := config.IsAuthenticated(userCfg)
36+
if err != nil {
37+
return err
38+
}
39+
if !connected {
40+
return errors.New("Not logged in")
41+
}
42+
43+
// Check if project exists
44+
if config.ProjectExists() {
45+
return errors.New("Project already exists")
46+
}
47+
48+
// Create project
49+
project, err := project.CreateProject(userCfg.GetString("server"), userCfg.GetString("jwt"), args[0])
50+
if err != nil {
51+
return err
52+
}
53+
54+
// Create project conf
55+
err = config.CreateProjectConfig()
56+
if err != nil {
57+
return err
58+
}
59+
60+
// Load project config
61+
projCfg, err := config.LoadProjectConfig()
62+
if err != nil {
63+
return err
64+
}
65+
66+
// Write config
67+
projCfg.Set("project", project)
68+
projCfg.WriteConfig()
69+
70+
// Add remote for paastech push
71+
_, err = repo.CreateRemote(&gitconf.RemoteConfig{
72+
Name: "paastech",
73+
URLs: []string{"ssh://git.paastech.cloud:8080/" + project.Id},
74+
})
75+
if err != nil {
76+
return err
77+
}
78+
79+
fmt.Println("Project " + project.Name + " created.")
80+
return nil
2581
},
2682
}
2783

cmd/login.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,16 @@ var loginCmd = &cobra.Command{
5454
}
5555

5656
// Load auth config
57-
err = config.LoadAuthConfig()
57+
userCfg, err := config.LoadAuthConfig()
5858
if err != nil {
5959
return err
6060
}
6161

6262
// Save jwt in auth conf
63-
config.SetAuth(server, jwt)
63+
userCfg.Set("server", server)
64+
userCfg.Set("jwt", jwt)
65+
userCfg.WriteConfig()
66+
6467
fmt.Println("✅ Login successful")
6568

6669
return nil

cmd/logout.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ import (
1010
var logoutCmd = &cobra.Command{
1111
GroupID: "account",
1212
Use: "logout",
13-
Short: "Log out from PaaSTech.cloud",
14-
Long: "Log out from PaaSTech.cloud",
13+
Short: "Log out from PaaSTech",
1514
RunE: func(cmd *cobra.Command, args []string) error {
1615
// Load auth config
17-
err := config.LoadAuthConfig()
16+
userCfg, err := config.LoadAuthConfig()
1817
if err != nil {
1918
return err
2019
}
2120

2221
// Save empty jwt in auth conf
23-
config.SetAuth("", "")
22+
userCfg.Set("server", "")
23+
userCfg.Set("jwt", "")
24+
userCfg.WriteConfig()
25+
2426
fmt.Println("🚪 Logging out")
2527

2628
return nil

0 commit comments

Comments
 (0)