Skip to content

Commit

Permalink
fix delete sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Jul 24, 2020
1 parent 00fccfa commit d9bd0f9
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<p align="center">A fast and beautiful command line tool to build API requests</p>
<p align="center">
<a href="https://travis-ci.com/Clivern/Poodle"><img src="https://travis-ci.com/Clivern/Poodle.svg?branch=master"></a>
<a href="https://github.com/Clivern/Poodle/releases"><img src="https://img.shields.io/badge/Version-1.0.1-red.svg"></a>
<a href="https://goreportcard.com/report/github.com/Clivern/Poodle"><img src="https://goreportcard.com/badge/github.com/Clivern/Poodle?v=1.0.1"></a>
<a href="https://github.com/Clivern/Poodle/releases"><img src="https://img.shields.io/badge/Version-2.0.0-red.svg"></a>
<a href="https://goreportcard.com/report/github.com/Clivern/Poodle"><img src="https://goreportcard.com/badge/github.com/Clivern/Poodle?v=2.0.0"></a>
<a href="https://github.com/Clivern/Poodle/blob/master/LICENSE"><img src="https://img.shields.io/badge/LICENSE-MIT-orange.svg"></a>
</p>
</p>
Expand Down
107 changes: 105 additions & 2 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package cmd

import (
"context"
"fmt"
"strings"
"time"
Expand All @@ -24,6 +25,7 @@ var deleteCmd = &cobra.Command{
Short: "Delete a service definition file",
Run: func(cmd *cobra.Command, args []string) {
var err error
var ok bool

if Verbose {
log.SetLevel(log.DebugLevel)
Expand Down Expand Up @@ -129,12 +131,113 @@ var deleteCmd = &cobra.Command{

err = util.DeleteFile(index[result])

spin.Stop()

if err != nil {
fmt.Printf("Error: %s", err.Error())
}

// Delete remotely
if conf.Gist.Username == "" || conf.Gist.AccessToken == "" {
spin.Stop()
fmt.Println(Green("Service file deleted successfully!"))
return
}

githubClient := module.NewGithubClient(
module.NewHTTPClient(),
module.GithubAPI,
conf.Gist.Username,
conf.Gist.AccessToken,
)

oauth, err := githubClient.Check(context.TODO())

if err != nil {
spin.Stop()
fmt.Println(Green("Service file deleted successfully!"))
return
}

if !oauth.Valid {
spin.Stop()
fmt.Println(Green("Service file deleted successfully!"))
return
}

remoteGist, err := githubClient.GetGist(context.TODO(), conf.Gist.GistID)

if err != nil {
fmt.Printf(
"Error while fetching remote gist: %s",
err.Error(),
)
return
}

remoteFs := module.NewFileSystem()

if _, found := remoteGist.Files["poodle"]; found {

ok, err = remoteFs.LoadFromJSON([]byte(remoteGist.Files["poodle"].Content))

if !ok || err != nil {
fmt.Printf(
"Error while loading remote fs from json: %s",
err.Error(),
)
return
}

delete(remoteFs.Files, strings.Replace(
index[result],
util.EnsureTrailingSlash(conf.Services.Directory),
"",
-1,
))

if err != nil {
fmt.Printf(
"Error while sync remote and local fs: %s",
err.Error(),
)
return
}

remoteData, err := remoteFs.ConvertToJSON()

if err != nil {
fmt.Printf(
"Error while converting remote data to json: %s",
err.Error(),
)
return
}

remoteGist.Files["poodle"] = module.File{
Content: remoteData,
Filename: "poodle",
}
}

_, err = githubClient.UpdateGist(
context.TODO(),
conf.Gist.GistID,
module.Gist{
Description: "Poodle",
Public: conf.Gist.Public,
Files: remoteGist.Files,
},
)

if err != nil {
fmt.Printf(
"Error while updating remote gist: %s",
err.Error(),
)
return
}

spin.Stop()

fmt.Println(Green("Service file deleted successfully!"))
},
}
Expand Down

0 comments on commit d9bd0f9

Please sign in to comment.