Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ GO_OBJECTS = \
commands/create_test.go \
commands/delete.go \
commands/delete_test.go \
commands/export.go \
commands/export_test.go \
commands/gc.go \
commands/gc_test.go \
commands/import.go \
commands/import_test.go \
commands/pull.go \
Expand Down
71 changes: 71 additions & 0 deletions commands/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2025 Miklos Vajna
//
// SPDX-License-Identifier: MIT

package commands

import (
"database/sql"
"encoding/json"
"fmt"

"github.com/spf13/cobra"
)

type passwordRow struct {
ID int
Machine string
Service string
User string
Password string
PasswordType PasswordType
Archived bool
Created string
Modified string
}

func exportPasswords(db *sql.DB) ([]byte, error) {
var results []passwordRow
rows, err := db.Query("select id, machine, service, user, password, type, archived, created, modified from passwords")
if err != nil {
return nil, fmt.Errorf("db.Query(select) failed: %s", err)
}

defer rows.Close()
for rows.Next() {
var row passwordRow
err = rows.Scan(&row.ID, &row.Machine, &row.Service, &row.User, &row.Password, &row.PasswordType, &row.Archived, &row.Created, &row.Modified)
if err != nil {
return nil, fmt.Errorf("rows.Scan() failed: %s", err)
}

results = append(results, row)
}

j, err := json.Marshal(results)
if err != nil {
return nil, fmt.Errorf("json.Marshal() failed: %s", err)
}

return j, nil
}

func newExportCommand(ctx *Context) *cobra.Command {
var cmd = &cobra.Command{
Use: "export",
Short: "exports passwords as JSON",
RunE: func(cmd *cobra.Command, args []string) error {
j, err := exportPasswords(ctx.Database)
if err != nil {
return fmt.Errorf("readPasswords() failed: %s", err)
}

fmt.Fprintf(cmd.OutOrStdout(), "%s\n", j)

ctx.NoWriteBack = true
return nil
},
}

return cmd
}
67 changes: 67 additions & 0 deletions commands/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2025 Miklos Vajna
//
// SPDX-License-Identifier: MIT

package commands

import (
"bytes"
"encoding/json"
"os"
"testing"
)

func TestExport(t *testing.T) {
ctx := CreateContextForTesting(t)
_, err := ctx.Database.Exec(`insert into passwords (machine, service, user, password, type) values('mymachine', 'myservice', 'myuser', 'mypassword', 'plain');`)
if err != nil {
t.Fatalf("createPassword() = %q, want nil", err)
}
os.Args = []string{"", "export"}
inBuf := new(bytes.Buffer)
outBuf := new(bytes.Buffer)

actualRet := Main(inBuf, outBuf)

expectedRet := 0
if actualRet != expectedRet {
t.Fatalf("Main() = %q, want %q", actualRet, expectedRet)
}
actualOutput := outBuf.String()
var passwords []passwordRow
err = json.NewDecoder(bytes.NewBufferString(actualOutput)).Decode(&passwords)
if err != nil {
t.Fatalf("json.Decode() = %q, want nil", err)
}
if len(passwords) != 1 {
t.Fatalf("passwords len = %q, want %q", len(passwords), 1)
}
password := passwords[0]
if password.ID != 1 {
t.Fatalf("password.ID = %q, want %q", password.ID, 1)
}
if password.Machine != "mymachine" {
t.Fatalf("password.Machine = %q, want %q", password.Machine, "mymachine")
}
if password.Service != "myservice" {
t.Fatalf("password.Service = %q, want %q", password.Service, "myservice")
}
if password.User != "myuser" {
t.Fatalf("password.User = %q, want %q", password.User, "myuser")
}
if password.Password != "mypassword" {
t.Fatalf("password.Password = %q, want %q", password.Password, "mypassword")
}
if password.PasswordType != "plain" {
t.Fatalf("password.PasswordType = %q, want %q", password.PasswordType, "plain")
}
if password.Archived != false {
t.Fatalf("password.Archived = %t, want %t", password.Archived, false)
}
if password.Created != "" {
t.Fatalf("password.Created = %q, want %q", password.Created, "")
}
if password.Modified != "" {
t.Fatalf("password.Modified = %q, want %q", password.Modified, "")
}
}
2 changes: 2 additions & 0 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func NewRootCommand(ctx *Context) *cobra.Command {
cmd.AddCommand(newPullCommand(ctx))
cmd.AddCommand(newVersionCommand(ctx))
cmd.AddCommand(newGcCommand(ctx))
cmd.AddCommand(newExportCommand(ctx))

return cmd
}
Expand All @@ -77,6 +78,7 @@ func getCommands() []string {
"update",
"version",
"gc",
"export",
}
}

Expand Down
4 changes: 4 additions & 0 deletions guide/src/news.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## main

- new `export` command to write the password database as a JSON file

## 25.8

- new `gc` command to rebuild the database file, repacking it into a minimal amount of disk space
Expand Down
10 changes: 2 additions & 8 deletions man/cpm-create.1
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
.nh
.TH "CPM" "1" "Jul 2022" "Auto generated by spf13/cobra" ""
.TH "CPM" "1" "Dec 2025" "Auto generated by spf13/cobra" ""

.SH NAME
.PP
cpm-create - creates a new password


.SH SYNOPSIS
.PP
\fBcpm create [flags]\fP


.SH DESCRIPTION
.PP
creates a new password


.SH OPTIONS
.PP
\fB-n\fP, \fB--dry-run\fP[=false]
do everything except actually perform the database action (default: false)

Expand Down Expand Up @@ -51,10 +47,8 @@ creates a new password


.SH SEE ALSO
.PP
\fBcpm(1)\fP


.SH HISTORY
.PP
22-Jul-2022 Auto generated by spf13/cobra
21-Dec-2025 Auto generated by spf13/cobra
10 changes: 2 additions & 8 deletions man/cpm-delete.1
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
.nh
.TH "CPM" "1" "Jul 2022" "Auto generated by spf13/cobra" ""
.TH "CPM" "1" "Dec 2025" "Auto generated by spf13/cobra" ""

.SH NAME
.PP
cpm-delete - deletes an existing password


.SH SYNOPSIS
.PP
\fBcpm delete [flags]\fP


.SH DESCRIPTION
.PP
deletes an existing password


.SH OPTIONS
.PP
\fB-n\fP, \fB--dry-run\fP[=false]
do everything except actually perform the database action (default: false)

Expand All @@ -31,10 +27,8 @@ deletes an existing password


.SH SEE ALSO
.PP
\fBcpm(1)\fP


.SH HISTORY
.PP
22-Jul-2022 Auto generated by spf13/cobra
21-Dec-2025 Auto generated by spf13/cobra
26 changes: 26 additions & 0 deletions man/cpm-export.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.nh
.TH "CPM" "1" "Dec 2025" "Auto generated by spf13/cobra" ""

.SH NAME
cpm-export - exports passwords as JSON


.SH SYNOPSIS
\fBcpm export [flags]\fP


.SH DESCRIPTION
exports passwords as JSON


.SH OPTIONS
\fB-h\fP, \fB--help\fP[=false]
help for export


.SH SEE ALSO
\fBcpm(1)\fP


.SH HISTORY
21-Dec-2025 Auto generated by spf13/cobra
10 changes: 2 additions & 8 deletions man/cpm-gc.1
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
.nh
.TH "CPM" "1" "Jul 2022" "Auto generated by spf13/cobra" ""
.TH "CPM" "1" "Dec 2025" "Auto generated by spf13/cobra" ""

.SH NAME
.PP
cpm-gc - rebuilds the database file, repacking it into a minimal amount of disk space


.SH SYNOPSIS
.PP
\fBcpm gc [flags]\fP


.SH DESCRIPTION
.PP
rebuilds the database file, repacking it into a minimal amount of disk space


.SH OPTIONS
.PP
\fB-h\fP, \fB--help\fP[=false]
help for gc


.SH SEE ALSO
.PP
\fBcpm(1)\fP


.SH HISTORY
.PP
22-Jul-2022 Auto generated by spf13/cobra
21-Dec-2025 Auto generated by spf13/cobra
10 changes: 2 additions & 8 deletions man/cpm-import.1
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
.nh
.TH "CPM" "1" "Jul 2022" "Auto generated by spf13/cobra" ""
.TH "CPM" "1" "Dec 2025" "Auto generated by spf13/cobra" ""

.SH NAME
.PP
cpm-import - imports an old XML database


.SH SYNOPSIS
.PP
\fBcpm import [flags]\fP


.SH DESCRIPTION
.PP
imports an old XML database


.SH OPTIONS
.PP
\fB-h\fP, \fB--help\fP[=false]
help for import


.SH SEE ALSO
.PP
\fBcpm(1)\fP


.SH HISTORY
.PP
22-Jul-2022 Auto generated by spf13/cobra
21-Dec-2025 Auto generated by spf13/cobra
10 changes: 2 additions & 8 deletions man/cpm-pull.1
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
.nh
.TH "CPM" "1" "Jul 2022" "Auto generated by spf13/cobra" ""
.TH "CPM" "1" "Dec 2025" "Auto generated by spf13/cobra" ""

.SH NAME
.PP
cpm-pull - copies a remote database to a local one


.SH SYNOPSIS
.PP
\fBcpm pull [flags]\fP


.SH DESCRIPTION
.PP
copies a remote database to a local one


.SH OPTIONS
.PP
\fB-h\fP, \fB--help\fP[=false]
help for pull


.SH SEE ALSO
.PP
\fBcpm(1)\fP


.SH HISTORY
.PP
22-Jul-2022 Auto generated by spf13/cobra
21-Dec-2025 Auto generated by spf13/cobra
Loading