Skip to content

Commit

Permalink
Add a new command to upload a license from a license payload string (m…
Browse files Browse the repository at this point in the history
…attermost#567)

* Add a new command to upload a license from a license payload string

* Fix

* Fix docs
  • Loading branch information
nickmisasi authored Oct 13, 2022
1 parent 4c28a0d commit 544e6e3
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
25 changes: 25 additions & 0 deletions commands/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ var UploadLicenseCmd = &cobra.Command{
RunE: withClient(uploadLicenseCmdF),
}

var UploadLicenseStringCmd = &cobra.Command{
Use: "upload-string [license]",
Short: "Upload a license from a string.",
Long: "Upload a license from a string. Replaces current license.",
Example: " license upload-string \"mylicensestring\"",
RunE: withClient(uploadLicenseStringCmdF),
}

var RemoveLicenseCmd = &cobra.Command{
Use: "remove",
Short: "Remove the current license.",
Expand All @@ -37,9 +45,26 @@ var RemoveLicenseCmd = &cobra.Command{
func init() {
LicenseCmd.AddCommand(UploadLicenseCmd)
LicenseCmd.AddCommand(RemoveLicenseCmd)
LicenseCmd.AddCommand(UploadLicenseStringCmd)
RootCmd.AddCommand(LicenseCmd)
}

func uploadLicenseStringCmdF(c client.Client, cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("enter one license file to upload")
}

licenseBytes := []byte(args[0])

if _, err := c.UploadLicenseFile(licenseBytes); err != nil {
return err
}

printer.Print("Uploaded license file")

return nil
}

func uploadLicenseCmdF(c client.Client, cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("enter one license file to upload")
Expand Down
25 changes: 25 additions & 0 deletions commands/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,28 @@ func (s *MmctlUnitTestSuite) TestUploadLicenseCmdF() {
s.Require().EqualError(err, "enter one license file to upload")
})
}

func (s *MmctlUnitTestSuite) TestUploadLicenseStringCmdF() {
// create temporary file
licenseString := string(fakeLicensePayload)

mockLicenseFile := []byte(fakeLicensePayload)

s.Run("Upload license successfully", func() {
printer.Clean()
s.client.
EXPECT().
UploadLicenseFile(mockLicenseFile).
Return(&model.Response{StatusCode: http.StatusOK}, nil).
Times(1)

err := uploadLicenseStringCmdF(s.client, &cobra.Command{}, []string{licenseString})
s.Require().Nil(err)
})

s.Run("Fail to upload license if no license string is given", func() {
printer.Clean()
err := uploadLicenseStringCmdF(s.client, &cobra.Command{}, []string{})
s.Require().EqualError(err, "enter one license file to upload")
})
}
1 change: 1 addition & 0 deletions docs/mmctl_license.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ SEE ALSO
* `mmctl <mmctl.rst>`_ - Remote client for the Open Source, self-hosted Slack-alternative
* `mmctl license remove <mmctl_license_remove.rst>`_ - Remove the current license.
* `mmctl license upload <mmctl_license_upload.rst>`_ - Upload a license.
* `mmctl license upload-string <mmctl_license_upload-string.rst>`_ - Upload a license from a string.

51 changes: 51 additions & 0 deletions docs/mmctl_license_upload-string.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
.. _mmctl_license_upload-string:

mmctl license upload-string
---------------------------

Upload a license from a string.

Synopsis
~~~~~~~~


Upload a license from a string. Replaces current license.

::

mmctl license upload-string [license] [flags]

Examples
~~~~~~~~

::

license upload-string "mylicensestring"

Options
~~~~~~~

::

-h, --help help for upload-string

Options inherited from parent commands
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

--config string path to the configuration file (default "$XDG_CONFIG_HOME/mmctl/config")
--disable-pager disables paged output
--insecure-sha1-intermediate allows to use insecure TLS protocols, such as SHA-1
--insecure-tls-version allows to use TLS versions 1.0 and 1.1
--json the output format will be in json format
--local allows communicating with the server through a unix socket
--quiet prevent mmctl to generate output for the commands
--strict will only run commands if the mmctl version matches the server one
--suppress-warnings disables printing warning messages

SEE ALSO
~~~~~~~~

* `mmctl license <mmctl_license.rst>`_ - Licensing commands

0 comments on commit 544e6e3

Please sign in to comment.