github.com/sqlitebrowser/dio@v0.0.0-20240125125356-b587368e5c6b/cmd/status.go (about)

     1  package cmd
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  // Displays whether a database has been modified since the last commit
    11  var statusCmd = &cobra.Command{
    12  	Use:   "status [database name]",
    13  	Short: "Displays whether a database has been modified since the last commit",
    14  	RunE: func(cmd *cobra.Command, args []string) error {
    15  		return status(args)
    16  	},
    17  }
    18  
    19  func init() {
    20  	RootCmd.AddCommand(statusCmd)
    21  }
    22  
    23  func status(args []string) error {
    24  	var db string
    25  	var err error
    26  	if len(args) == 0 {
    27  		// TODO: If no database name is given, we should show the status for all known databases (eg in local .dio cache)
    28  		//       in the current directory instead
    29  		db, err = getDefaultDatabase()
    30  		if err != nil {
    31  			return err
    32  		}
    33  		if db == "" {
    34  			// No database name was given on the command line, and we don't have a default database selected
    35  			return errors.New("No database file specified")
    36  		}
    37  	} else {
    38  		db = args[0]
    39  	}
    40  	// TODO: Allow giving multiple database files on the command line.  Hopefully just needs turning this
    41  	// TODO  into a for loop
    42  	if len(args) > 1 {
    43  		return errors.New("Only one database can be worked with at a time (for now)")
    44  	}
    45  
    46  	// If there is a local metadata cache for the requested database, use that.  Otherwise, retrieve it from the
    47  	// server first (without storing it)
    48  	var meta metaData
    49  	meta, err = localFetchMetadata(db, true)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	// Check if the file has changed, and let the user know
    55  	changed, err := dbChanged(db, meta)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	if changed {
    60  		_, err = fmt.Fprintf(fOut, "  * '%s': has been changed\n", db)
    61  		if err != nil {
    62  			return err
    63  		}
    64  		return nil
    65  	}
    66  	_, err = fmt.Fprintf(fOut, "  * '%s': unchanged\n", db)
    67  	return err
    68  }