Skip to content

Commit

Permalink
Add apply entry and apply folder commands
Browse files Browse the repository at this point in the history
  • Loading branch information
marevers committed Oct 13, 2023
1 parent 563bbeb commit 1db1ac4
Show file tree
Hide file tree
Showing 3 changed files with 307 additions and 0 deletions.
137 changes: 137 additions & 0 deletions cmd/apply-entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
Copyright © 2023 Martijn Evers
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 (
"fmt"

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// applyEntryCmd represents the entry command
var applyEntryCmd = &cobra.Command{
Use: "entry",
Short: "Applies a configuration to an entry",
Long: `Applies a configuration to an entry in Pleasant Password Server. Takes a JSON string as input.
If the entry does not exist, it is created using the supplied configuration
and the command returns the entry's id. If it exists, any changes will be applied to the entry.
Partial updates are allowed e.g. only supplying 'Name' and 'Password' to update the password.
Note: you cannot use this command to change the name of an entry.
'GroupId' can be omitted if the path of the entry is supplied.
Examples:
pleasant-cli apply entry --data '
{
"Name": "TestEntry",
"Username": "MyNewUserName",
"Password": "MyNewPassword01",
"GroupId": "c04f874b-90f7-4b33-97d0-a92e011fb712"
}'
pleasant-cli apply entry --path 'Root/Folder1/TestEntry' --data '
{
"Name": "TestEntry",
"Username": "MyNewUserName",
"Password": "MyNewPassword01"
}'`,
Run: func(cmd *cobra.Command, args []string) {
if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) {
return
}

baseUrl := viper.GetString("serverurl")
bearerToken := viper.GetString("bearertoken.accesstoken")

json, err := cmd.Flags().GetString("data")
if err != nil {
fmt.Println(err)
return
}

input, err := pleasant.UnmarshalEntry(json)
if err != nil {
fmt.Println(err)
return
}

if cmd.Flags().Changed("path") {
resourcePath, err := cmd.Flags().GetString("path")
if err != nil {
fmt.Println(err)
return
}

pid, err := pleasant.GetParentIdByResourcePath(baseUrl, resourcePath, bearerToken)
if err != nil {
fmt.Println(err)
return
}

if !pleasant.PathAndNameMatching(resourcePath, input.Name) {
fmt.Println("error: entry name from path and data do not match")
return
}

input.GroupId = pid

j, err := pleasant.MarshalEntry(input)
if err != nil {
fmt.Println(err)
return
}

json = j
}

id, err := pleasant.DuplicateEntryId(baseUrl, json, bearerToken)
if err != nil {
fmt.Println(err)
return
}

if id != "" {
subPath := pleasant.PathEntry + "/" + id

_, err := pleasant.PatchJsonString(baseUrl, subPath, json, bearerToken)
if err != nil {
fmt.Println(err)
return
}

fmt.Println("Existing entry with id", id, "patched")
return
}

id, err = pleasant.PostJsonString(baseUrl, pleasant.PathEntry, json, bearerToken)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(id)
},
}

func init() {
applyCmd.AddCommand(applyEntryCmd)

applyEntryCmd.Flags().StringP("data", "d", "", "JSON string with entry data")
applyEntryCmd.Flags().StringP("path", "p", "", "Path to entry")
applyEntryCmd.MarkFlagRequired("data")
}
135 changes: 135 additions & 0 deletions cmd/apply-folder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
Copyright © 2023 Martijn Evers
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 (
"fmt"

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// applyFolderCmd represents the folder command
var applyFolderCmd = &cobra.Command{
Use: "folder",
Short: "Applies a configuration to a folder",
Long: `Applies a configuration to a folder in Pleasant Password Server. Takes a JSON string as input.
If the folder does not exist, it is created using the supplied configuration
and the command returns the folder's id. If it exists, any changes will be applied to the folder.
Partial updates are allowed e.g. only supplying 'Name' and 'Notes' to update the notes.
Note: you cannot use this command to change the name of a folder.
'ParentId' can be omitted if the path of the folder is supplied.
Examples:
pleasant-cli apply folder --data '
{
"Notes": "New note for the folder",
"Name": "TestFolder",
"ParentId": "c04f874b-90f7-4b33-97d0-a92e011fb712"
}'
pleasant-cli apply folder --path 'Root/Folder1/TestFolder' --data '
{
"Notes": "New note for the folder",
"Name": "TestFolder"
}'`,
Run: func(cmd *cobra.Command, args []string) {
if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) {
return
}

baseUrl := viper.GetString("serverurl")
bearerToken := viper.GetString("bearertoken.accesstoken")

json, err := cmd.Flags().GetString("data")
if err != nil {
fmt.Println(err)
return
}

input, err := pleasant.UnmarshalFolder(json)
if err != nil {
fmt.Println(err)
return
}

if cmd.Flags().Changed("path") {
resourcePath, err := cmd.Flags().GetString("path")
if err != nil {
fmt.Println(err)
return
}

pid, err := pleasant.GetParentIdByResourcePath(baseUrl, resourcePath, bearerToken)
if err != nil {
fmt.Println(err)
return
}

if !pleasant.PathAndNameMatching(resourcePath, input.Name) {
fmt.Println("error: folder name from path and data do not match")
return
}

input.ParentId = pid

j, err := pleasant.MarshalFolder(input)
if err != nil {
fmt.Println(err)
return
}

json = j
}

id, err := pleasant.DuplicateFolderId(baseUrl, json, bearerToken)
if err != nil {
fmt.Println(err)
return
}

if id != "" {
subPath := pleasant.PathFolders + "/" + id

_, err := pleasant.PatchJsonString(baseUrl, subPath, json, bearerToken)
if err != nil {
fmt.Println(err)
return
}

fmt.Println("Existing folder with id", id, "patched")
return
}

id, err = pleasant.PostJsonString(baseUrl, pleasant.PathFolders, json, bearerToken)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(id)
},
}

func init() {
applyCmd.AddCommand(applyFolderCmd)

applyFolderCmd.Flags().StringP("data", "d", "", "JSON string with folder data")
applyFolderCmd.Flags().StringP("path", "p", "", "Path to folder")
applyFolderCmd.MarkFlagRequired("data")
}
35 changes: 35 additions & 0 deletions cmd/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright © 2023 Martijn Evers
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 (
"github.com/spf13/cobra"
)

// applyCmd represents the apply command
var applyCmd = &cobra.Command{
Use: "apply",
Short: "Applies a configuration to entries or folders",
Long: `Applies a configuration to entries or folders.
Creates the entry or folder if it does not yet exist.`,
Args: cobra.MatchAll(cobra.MinimumNArgs(1)),
Run: func(cmd *cobra.Command, args []string) {
},
}

func init() {
rootCmd.AddCommand(applyCmd)
}

0 comments on commit 1db1ac4

Please sign in to comment.