github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/cmd/mattermost/commands/license.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"errors"
     8  	"io/ioutil"
     9  
    10  	"github.com/spf13/cobra"
    11  
    12  	"github.com/mattermost/mattermost-server/v5/audit"
    13  )
    14  
    15  var LicenseCmd = &cobra.Command{
    16  	Use:   "license",
    17  	Short: "Licensing commands",
    18  }
    19  
    20  var UploadLicenseCmd = &cobra.Command{
    21  	Use:     "upload [license]",
    22  	Short:   "Upload a license.",
    23  	Long:    "Upload a license. Replaces current license.",
    24  	Example: "  license upload /path/to/license/mylicensefile.mattermost-license",
    25  	RunE:    uploadLicenseCmdF,
    26  }
    27  
    28  func init() {
    29  	LicenseCmd.AddCommand(UploadLicenseCmd)
    30  	RootCmd.AddCommand(LicenseCmd)
    31  }
    32  
    33  func uploadLicenseCmdF(command *cobra.Command, args []string) error {
    34  	a, err := InitDBCommandContextCobra(command)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	defer a.Srv().Shutdown()
    39  
    40  	if len(args) != 1 {
    41  		return errors.New("Enter one license file to upload")
    42  	}
    43  
    44  	var fileBytes []byte
    45  	if fileBytes, err = ioutil.ReadFile(args[0]); err != nil {
    46  		return err
    47  	}
    48  
    49  	if _, err := a.Srv().SaveLicense(fileBytes); err != nil {
    50  		return err
    51  	}
    52  
    53  	CommandPrettyPrintln("Uploaded license file")
    54  
    55  	auditRec := a.MakeAuditRecord("uploadLicense", audit.Success)
    56  	auditRec.AddMeta("file", args[0])
    57  	a.LogAuditRec(auditRec, nil)
    58  
    59  	return nil
    60  }