Skip to content

Commit

Permalink
Update to add remove-from-repo command (appsody#829)
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Groeger <[email protected]>

Co-authored-by: Kyle G. Christianson <[email protected]>
  • Loading branch information
groeges and kylegc committed Jan 8, 2020
1 parent c501ed3 commit b2eeca5
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cmd/stack.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2019 IBM Corporation and others.
// Copyright © 2019, 2020 IBM Corporation and others.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,5 +29,6 @@ func newStackCmd(rootConfig *RootCommandConfig) *cobra.Command {
stackCmd.AddCommand(newStackValidateCmd(rootConfig))
stackCmd.AddCommand(newStackPackageCmd(rootConfig))
stackCmd.AddCommand(newStackAddToRepoCmd(rootConfig))
stackCmd.AddCommand(newStackRemoveFromRepoCmd(rootConfig))
return stackCmd
}
176 changes: 176 additions & 0 deletions cmd/stack_removefromrepo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// Copyright © 2020 IBM Corporation and others.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd

import (
"os"
"strings"

"io/ioutil"
"path/filepath"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"gopkg.in/yaml.v2"
)

func newStackRemoveFromRepoCmd(rootConfig *RootCommandConfig) *cobra.Command {
var stackName string
var repoName string
var useLocalCache bool

log := rootConfig.LoggingConfig

var stackRemoveFromRepoCmd = &cobra.Command{
Use: "remove-from-repo <repo-name> <stack-name>",
Short: "Remove stack information from an Appsody repository",
Long: `Removes stack information from an Appsody repository.
Removes stack information, specified by <stack-name> from an Appsody repository, specified by the <repo-name> argument.
The updated repository index file is created in ~/.appsody/stacks/dev.local directory.`,

Example: ` appsody stack remove-from-repo incubator nodejs
Updates the repository index file for the incubator repository, removing the definition of the nodejs stack`,
RunE: func(cmd *cobra.Command, args []string) error {

log.Info.Log("******************************************")
log.Info.Log("Running appsody stack remove-from-repo")
log.Info.Log("******************************************")

if len(args) < 2 {
return errors.New("Required parameter missing. You must specify a repository name and a stack name")
}

repoName = args[0]
stackName = args[1]

log.Debug.Log("repoName is: ", repoName)
log.Debug.Log("stackName is: ", stackName)

var repoFile RepositoryFile

appsodyHome := getHome(rootConfig)
log.Debug.Log("appsodyHome is:", appsodyHome)

devLocal := filepath.Join(appsodyHome, "stacks", "dev.local")
log.Debug.Log("devLocal is: ", devLocal)

// create the devLocal directory in appsody home
err := os.MkdirAll(devLocal, os.FileMode(0755))
if err != nil {
return errors.Errorf("Error creating directory: %v", err)
}

localIndexFile := filepath.Join(devLocal, repoName+"-index.yaml")
log.Debug.Log("localIndexFile is: ", localIndexFile)

var indexYaml IndexYaml

_, repoErr := repoFile.getRepos(rootConfig)
if repoErr != nil {
return repoErr
}

if repoFile.Has(repoName) {
// The repoName exists within the repository list
log.Debug.Log(repoName, " exists within the repository list")
repo := repoFile.GetRepo(repoName)
url := repo.URL
log.Debug.Log(repoName, " URL is: ", url)

// Check if the repo URL points to a remote repository
if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
log.Debug.log("Remote repository index url: ", url)
// Check to see whether the local index file exists
exists, err := Exists(localIndexFile)
if err != nil {
return errors.Errorf("Error checking status of %s", localIndexFile)
}
if exists && useLocalCache {
log.Debug.Log(localIndexFile, " exists in the appsody directory and use-local-cache is true")
} else {
// local file doesnt exist, download the index from the repote location and use it
log.Debug.Log(localIndexFile, " doesnt exist in the appsody directory")
log.Info.Log("Downloading the remote index file from: ", url)
log.Info.log("Creating repository index file: ", localIndexFile)
err := downloadFileToDisk(log, url, localIndexFile, false)
if err != nil {
return err
}
}
} else {
log.Debug.log("Local repository index url: ", url)
localIndexFile = strings.TrimPrefix(url, "file://")
exists, err := Exists(localIndexFile)
if err != nil {
return errors.Errorf("Error checking status of %s", localIndexFile)
}
if exists && useLocalCache {
log.Debug.Log(localIndexFile, " exists in the appsody directory and use-local-cache is true")
} else {
log.Info.Log("Repository index file not found - unable to remove stack")
return nil
}
}
} else {
log.Debug.Log(repoName, " does not exist within the repository list")
exists, err := Exists(localIndexFile)
if err != nil {
return errors.Errorf("Error checking status of %s", localIndexFile)
}
if exists && useLocalCache {
log.Debug.Log(localIndexFile, " exists in the appsody directory and use-local-cache is true")
} else {
log.Info.Log("Repository index file not found - unable to remove stack")
return nil
}
}

log.Info.log("Updating repository index file: ", localIndexFile)
source, err := ioutil.ReadFile(localIndexFile)
if err != nil {
return errors.Errorf("Error trying to read: %v", err)
}
err = yaml.Unmarshal(source, &indexYaml)
if err != nil {
return errors.Errorf("Error trying to unmarshall: %v", err)
}

// At this point we should have the indexFile loaded that want to use for updating / adding stack info
// find the index of the stack
indexYaml = findStackAndRemove(log, stackName, indexYaml)

// Last thing to do is write the data to the file
data, err := yaml.Marshal(indexYaml)
if err != nil {
return err
}

err = ioutil.WriteFile(localIndexFile, data, 0666)
if err != nil {
return errors.Errorf("Error writing localIndexFile: %v", err)
}

log.Info.Log("Repository index file updated successfully")

return nil
},
}

stackRemoveFromRepoCmd.PersistentFlags().BoolVar(&useLocalCache, "use-local-cache", false, "Whether to use a local file if exists or create a new file")

return stackRemoveFromRepoCmd
}

0 comments on commit b2eeca5

Please sign in to comment.