-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): ssh cli api and middleware
- Loading branch information
1 parent
d64549c
commit c1cbb40
Showing
17 changed files
with
732 additions
and
497 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/charmbracelet/soft-serve/git" | ||
gitm "github.com/gogs/git-module" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func branchCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "branch", | ||
Short: "Manage repository branches", | ||
} | ||
|
||
cmd.AddCommand( | ||
branchListCommand(), | ||
branchDefaultCommand(), | ||
branchDeleteCommand(), | ||
) | ||
|
||
return cmd | ||
} | ||
|
||
func branchListCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list REPOSITORY", | ||
Short: "List repository branches", | ||
Args: cobra.ExactArgs(1), | ||
PersistentPreRunE: checkIfReadable, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cfg, _ := fromContext(cmd) | ||
rn := strings.TrimSuffix(args[0], ".git") | ||
rr, err := cfg.Backend.Repository(rn) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r, err := rr.Open() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
branches, _ := r.Branches() | ||
for _, b := range branches { | ||
cmd.Println(b) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func branchDefaultCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "default REPOSITORY [BRANCH]", | ||
Short: "Set or get the default branch", | ||
Args: cobra.RangeArgs(1, 2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cfg, _ := fromContext(cmd) | ||
rn := strings.TrimSuffix(args[0], ".git") | ||
switch len(args) { | ||
case 1: | ||
if err := checkIfReadable(cmd, args); err != nil { | ||
return err | ||
} | ||
rr, err := cfg.Backend.Repository(rn) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r, err := rr.Open() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
head, err := r.HEAD() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cmd.Println(head.Name().Short()) | ||
case 2: | ||
if err := checkIfCollab(cmd, args); err != nil { | ||
return err | ||
} | ||
|
||
rr, err := cfg.Backend.Repository(rn) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r, err := rr.Open() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
branch := args[1] | ||
branches, _ := r.Branches() | ||
var exists bool | ||
for _, b := range branches { | ||
if branch == b { | ||
exists = true | ||
break | ||
} | ||
} | ||
|
||
if !exists { | ||
return git.ErrReferenceNotExist | ||
} | ||
|
||
if _, err := r.SymbolicRef("HEAD", gitm.RefsHeads+branch); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func branchDeleteCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "delete REPOSITORY BRANCH", | ||
Aliases: []string{"remove", "rm", "del"}, | ||
Short: "Delete a branch", | ||
PersistentPreRunE: checkIfCollab, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cfg, _ := fromContext(cmd) | ||
rn := strings.TrimSuffix(args[0], ".git") | ||
rr, err := cfg.Backend.Repository(rn) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r, err := rr.Open() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
branch := args[1] | ||
branches, _ := r.Branches() | ||
var exists bool | ||
for _, b := range branches { | ||
if branch == b { | ||
exists = true | ||
break | ||
} | ||
} | ||
|
||
if !exists { | ||
return git.ErrReferenceNotExist | ||
} | ||
|
||
head, err := r.HEAD() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if head.Name().Short() == branch { | ||
return fmt.Errorf("cannot delete the default branch") | ||
} | ||
|
||
if err := r.DeleteBranch(branch, gitm.DeleteBranchOptions{Force: true}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cmd | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
// createCommand is the command for creating a new repository. | ||
func createCommand() *cobra.Command { | ||
var private bool | ||
var description string | ||
cmd := &cobra.Command{ | ||
Use: "create REPOSITORY", | ||
Short: "Create a new repository.", | ||
Args: cobra.ExactArgs(1), | ||
PersistentPreRunE: checkIfAdmin, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cfg, _ := fromContext(cmd) | ||
name := args[0] | ||
if _, err := cfg.Backend.CreateRepository(name, private); err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().BoolVarP(&private, "private", "p", false, "make the repository private") | ||
cmd.Flags().StringVarP(&description, "description", "d", "", "set the repository description") | ||
return cmd | ||
} |
Oops, something went wrong.