cmd

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CreateCmd = &cobra.Command{
	Use:   "create [location to put database]",
	Short: "Create a new database at the specified location",
	Long: `Sets up the given directory with a new heedy database.
Creates the folder if it doesn't exist, but fails if the folder is not empty. If no folder is specified, uses the default database location.

If you want to set it up from command line, without the setup server, you can specify a configuration file and the admin user:
   
  heedy create ./myfolder --noserver -c ./heedy.conf --username=myusername --password=mypassword

It is recommended that new users use the web setup, which will guide you in preparing the database for use:

  heedy create ./myfolder

`,
	RunE: func(cmd *cobra.Command, args []string) error {
		directory, err := GetDirectory(args)
		if err != nil {
			return err
		}

		c := assets.NewConfiguration()
		c.Addr = &addr
		c.Verbose = verbose
		if loglevel != "" {
			c.LogLevel = &loglevel
		}
		if logdir != "" {
			c.LogDir = &logdir
		}
		if len(plugins) > 0 {
			plugin_names := make([]string, len(plugins))
			for i, p := range plugins {
				plugin_names[i] = path.Base(p)
			}
			c.ActivePlugins = &plugin_names
		}

		sc := server.SetupContext{
			CreateOptions: assets.CreateOptions{
				Config:     c,
				Directory:  directory,
				ConfigFile: configFile,
				Plugins:    plugins,
			},
			User: server.SetupUser{
				UserName: username,
				Password: password,
			},
		}

		if noserver {
			err := server.SetupCreate(sc)
			if err == nil && testapp != "" {

				db, err := database.Open(assets.Get())
				if err != nil {
					os.RemoveAll(directory)
					return err
				}
				defer db.Close()
				appname := "Test App"
				appid, _, err := db.CreateApp(&database.App{
					Details: database.Details{
						Name: &appname,
					},
					Owner: &sc.User.UserName,
					Scope: &database.AppScopeArray{
						ScopeArray: database.ScopeArray{
							Scope: []string{"*"},
						},
					},
				})
				if err != nil {
					os.RemoveAll(directory)
					return err
				}

				_, err = db.Exec("UPDATE apps SET access_token=? WHERE id=?;", testapp, appid)
				return err
			}
			return err
		} else if testapp != "" {
			return errors.New("testapp can only be set in noserver mode")
		}
		return server.Setup(sc)

	},
}

CreateCmd creates a new database

View Source
var (

	// ErrTooManyArgs is called when given too many args
	ErrTooManyArgs = errors.New("Too many arguments were specified.")
)
View Source
var RootCmd = &cobra.Command{
	Use:     "heedy",
	Short:   "Heedy is a personal data repository and analysis system",
	Long:    `Heedy is an aggregator and dashboard for storing and visualizing data gathered by various trackers. It is buit to be extensible and self-contained, with a powerful plugin system allowing for in-depth analysis and action.`,
	Version: buildinfo.Version,
	RunE: func(cmd *cobra.Command, args []string) error {
		c := assets.NewConfiguration()
		c.Verbose = verbose
		if loglevel != "" {
			c.LogLevel = &loglevel
		}
		if logdir != "" {
			c.LogDir = &logdir
		}

		directory, err := UserDataDir()
		if err != nil {
			return err
		}
		directory = path.Join(directory, "heedy")
		if _, err := os.Stat(path.Join(directory, "heedy.conf")); os.IsNotExist(err) {

			return server.Setup(server.SetupContext{
				CreateOptions: assets.CreateOptions{
					Config:    c,
					Directory: directory,
				},
			})
		}

		directory, err = filepath.Abs(directory)
		if err != nil {
			return err
		}
		logrus.Infof("Using database at %s", directory)
		if err = writepid(directory); err != nil {
			return err
		}
		defer delpid(directory)

		return updater.Run(updater.Options{
			ConfigDir:   directory,
			AddonConfig: c,
			Revert:      revert,
			Runner: func(a *assets.Assets) error {
				return server.Run(a, nil)
			},
			Update: applyUpdates,
		})
	},
	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
		if loglevel != "" {
			log_level, err := logrus.ParseLevel(loglevel)
			if err != nil {
				return err
			}
			logrus.SetLevel(log_level)
		}
		if verbose {
			logrus.SetLevel(logrus.DebugLevel)
		}
		if logdir != "" && logdir != "stdout" {
			// The log file will be opened in assets. However, assets use the database dir as pwd
			// so we want to convert the file path to relative to current folder
			var err error
			logdir, err = filepath.Abs(logdir)
			if err != nil {
				return err
			}
		}

		if cpuprofile != "" {
			logrus.Warnf("Creating CPU Profile at '%s'", cpuprofile)
			f, err := os.Create(cpuprofile)
			if err != nil {
				return err
			}
			if err := pprof.StartCPUProfile(f); err != nil {
				return err
			}
			cpuprofileFile = f
		}

		if buildinfo.DevMode {
			logrus.Warn("Running heedy in development mode")
		}

		return nil
	},
	PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
		if cpuprofile != "" {
			pprof.StopCPUProfile()
			cpuprofileFile.Close()
		}
		if memprofile != "" {
			logrus.Warnf("Creating Memory Profile at '%s'", memprofile)
			f, err := os.Create(memprofile)
			if err != nil {
				return err
			}
			defer f.Close()
			runtime.GC()
			if err = pprof.WriteHeapProfile(f); err != nil {
				return err
			}
		}

		return nil
	},
}

RootCmd is the root command under which all other commands are placed. It is used to initialize all variables that are global for the whole app

View Source
var RunCmd = &cobra.Command{
	Use:   "run [location of database]",
	Short: "Runs existing heedy database",
	Long:  `Runs heedy using the passed database. If no folder is specifed, uses the default database location.`,
	RunE: func(cmd *cobra.Command, args []string) error {
		directory, err := GetDirectory(args)
		if err != nil {
			return err
		}
		c := assets.NewConfiguration()
		c.Verbose = verbose
		if loglevel != "" {
			c.LogLevel = &loglevel
		}
		if logdir != "" {
			c.LogDir = &logdir
		}

		if _, err := os.Stat(path.Join(directory, "heedy.conf")); os.IsNotExist(err) {

			if !createIfNotExists {
				return fmt.Errorf("no database found at %s", directory)
			}

			return server.Setup(server.SetupContext{
				CreateOptions: assets.CreateOptions{
					Config:    c,
					Directory: directory,
				},
			})
		}

		if err = writepid(directory); err != nil {
			return err
		}
		defer delpid(directory)

		return updater.Run(updater.Options{
			ConfigDir:   directory,
			AddonConfig: c,
			Revert:      revert,
			Update:      applyUpdates,
			Runner: func(a *assets.Assets) error {
				return server.Run(a, nil)
			},
		})
	},
}
View Source
var StartCmd = &cobra.Command{
	Use:   "start [location of database]",
	Short: "Starts heedy in the background",
	Long:  `Starts a background heedy process using the passed database. If not folder is specified, uses the default database location.`,
	RunE: func(cmd *cobra.Command, args []string) error {
		directory, err := GetDirectory(args)
		if err != nil {
			return err
		}

		return updater.StartHeedy(directory, false)
	},
}
View Source
var StopCmd = &cobra.Command{
	Use:   "stop [location of database]",
	Short: "Stops a heedy server running in the background",
	Long:  `Shuts down heedy running in the background, the main way to stop servers started using 'heedy start'.`,
	RunE: func(cmd *cobra.Command, args []string) error {
		directory, err := GetDirectory(args)
		if err != nil {
			return err
		}

		p, err := getpid(directory)
		if err != nil {
			return err
		}
		logrus.Infof("Sending SIGINT to %d", p.Pid)
		return p.Signal(os.Interrupt)
	},
}
View Source
var VersionCmd = &cobra.Command{
	Use:   "version",
	Short: "Shows detailed version information",
	Long:  "Shows heedy's compilation and version details",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Printf("Heedy v%s\n", buildinfo.Version)

		fmt.Printf(`
Built on:    %s
Git Hash:    %s
Go Version:  %s
Arch:        %s/%s
`, buildinfo.BuildTimestamp, buildinfo.GitHash, runtime.Version(), runtime.GOOS, runtime.GOARCH)
		if verbose {
			bi, ok := debug.ReadBuildInfo()
			if ok {
				fmt.Println("\nBuild Deps:")
				for _, d := range bi.Deps {
					fmt.Printf("%s %s (%s)\n", d.Path, d.Version, d.Sum)
				}
			}
		}
	},
}

Functions

func Execute

func Execute()

Execute runs the root command

func GetDirectory

func GetDirectory(args []string) (string, error)

func UserDataDir

func UserDataDir() (string, error)

UserDataDir is identical to os.UserConfigDir, but returns the linux app data folder instead of config folder

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL