diff --git a/.gitignore b/.gitignore index d13e0125..c6c3e235 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ cover.out mc +*~ diff --git a/cp-json.go b/cp-json.go new file mode 100644 index 00000000..d114f13d --- /dev/null +++ b/cp-json.go @@ -0,0 +1,41 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "encoding/json" + "fmt" +) + +// CopyMessage container for file copy messages +type CopyMessage struct { + Source string `json:"source"` + Target string `json:"target"` + Length int64 `json:"length"` +} + +// String string printer for copy message +func (c CopyMessage) String() string { + if !globalJSONFlag { + return fmt.Sprintf("‘%s’ -> ‘%s’\n", c.Source, c.Target) + } + copyMessageBytes, err := json.Marshal(c) + if err != nil { + panic(err) + } + return string(copyMessageBytes) + "\n" +} diff --git a/diff-json.go b/diff-json.go new file mode 100644 index 00000000..7689f41e --- /dev/null +++ b/diff-json.go @@ -0,0 +1,51 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import "encoding/json" + +// DiffJSONMessage json container for diff messages +type DiffJSONMessage struct { + FirstURL string `json:"first"` + SecondURL string `json:"second"` + Diff string `json:"diff"` +} + +func (s diffV1) String() string { + if !globalJSONFlag { + var message string + if s.diffType == "Only-in" { + message = "‘" + s.firstURL + "’ Only in ‘" + s.secondURL + "’\n" + } + if s.diffType == "Type" { + message = s.firstURL + " and " + s.secondURL + " differs in type.\n" + } + if s.diffType == "Size" { + message = s.firstURL + " and " + s.secondURL + " differs in size.\n" + } + return message + } + diffMessage := DiffJSONMessage{} + diffMessage.FirstURL = s.firstURL + diffMessage.SecondURL = s.secondURL + diffMessage.Diff = s.diffType + diffJSONBytes, err := json.Marshal(diffMessage) + if err != nil { + panic(err) + } + return string(diffJSONBytes) + "\n" +} diff --git a/ls-json.go b/ls-json.go new file mode 100644 index 00000000..47c7a565 --- /dev/null +++ b/ls-json.go @@ -0,0 +1,51 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "encoding/json" + + "github.com/minio/mc/pkg/console" +) + +// ContentMessage container for content message structure +type ContentMessage struct { + Filetype string `json:"type"` + Time string `json:"last-modified"` + Size string `json:"size"` + Name string `json:"name"` +} + +// String string printer for Content metadata +func (c ContentMessage) String() string { + if !globalJSONFlag { + message := console.Time("[%s] ", c.Time) + message = message + console.Size("%6s ", c.Size) + message = func() string { + if c.Filetype == "folder" { + return message + console.Dir("%s", c.Name) + } + return message + console.File("%s", c.Name) + }() + return message + "\n" + } + jsonMessageBytes, err := json.Marshal(c) + if err != nil { + panic(err) + } + return string(jsonMessageBytes) + "\n" +} diff --git a/mirror-json.go b/mirror-json.go new file mode 100644 index 00000000..8aaf645c --- /dev/null +++ b/mirror-json.go @@ -0,0 +1,41 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "encoding/json" + "fmt" +) + +// MirrorMessage container for file mirror messages +type MirrorMessage struct { + Source string `json:"source"` + Targets []string `json:"targets"` + Length int64 `json:"length"` +} + +// String string printer for mirror message +func (s MirrorMessage) String() string { + if !globalJSONFlag { + return fmt.Sprintf("‘%s’ -> ‘%s’\n", s.Source, s.Targets) + } + mirrorMessageBytes, err := json.Marshal(s) + if err != nil { + panic(err) + } + return string(mirrorMessageBytes) + "\n" +} diff --git a/print-structs.go b/print-structs.go deleted file mode 100644 index 0aed9ac3..00000000 --- a/print-structs.go +++ /dev/null @@ -1,175 +0,0 @@ -/* - * Minio Client (C) 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// This package contains all the structs, their method wrappers for printer -package main - -import ( - "bytes" - "encoding/json" - "fmt" - "strings" - "time" - - "github.com/minio/mc/pkg/console" -) - -// DiffJSONMessage json container for diff messages -type DiffJSONMessage struct { - FirstURL string `json:"first"` - SecondURL string `json:"second"` - Diff string `json:"diff"` -} - -func (s diffV1) String() string { - if !globalJSONFlag { - var message string - if s.diffType == "Only-in" { - message = "‘" + s.firstURL + "’ Only in ‘" + s.secondURL + "’\n" - } - if s.diffType == "Type" { - message = s.firstURL + " and " + s.secondURL + " differs in type.\n" - } - if s.diffType == "Size" { - message = s.firstURL + " and " + s.secondURL + " differs in size.\n" - } - return message - } - diffMessage := DiffJSONMessage{} - diffMessage.FirstURL = s.firstURL - diffMessage.SecondURL = s.secondURL - diffMessage.Diff = s.diffType - diffJSONBytes, err := json.Marshal(diffMessage) - if err != nil { - panic(err) - } - return string(diffJSONBytes) + "\n" -} - -// SessionJSONMessage json container for session messages -type SessionJSONMessage struct { - SessionID string `json:"sessionid"` - Time string `json:"time"` - CommandType string `json:"command-type"` - CommandArgs []string `json:"command-args"` -} - -func (s sessionV2) String() string { - if !globalJSONFlag { - message := console.SessionID("%s -> ", s.SessionID) - message = message + console.Time("[%s]", s.Header.When.Local().Format(printDate)) - message = message + console.Command(" %s %s", s.Header.CommandType, strings.Join(s.Header.CommandArgs, " ")) - return message + "\n" - } - sessionMesage := SessionJSONMessage{ - SessionID: s.SessionID, - Time: s.Header.When.Local().Format(printDate), - CommandType: s.Header.CommandType, - CommandArgs: s.Header.CommandArgs, - } - sessionJSONBytes, err := json.Marshal(sessionMesage) - if err != nil { - panic(err) - } - return string(sessionJSONBytes) + "\n" -} - -// ContentMessage container for content message structure -type ContentMessage struct { - Filetype string `json:"type"` - Time string `json:"last-modified"` - Size string `json:"size"` - Name string `json:"name"` -} - -// String string printer for Content metadata -func (c ContentMessage) String() string { - if !globalJSONFlag { - message := console.Time("[%s] ", c.Time) - message = message + console.Size("%6s ", c.Size) - message = func() string { - if c.Filetype == "folder" { - return message + console.Dir("%s", c.Name) - } - return message + console.File("%s", c.Name) - }() - return message + "\n" - } - jsonMessageBytes, err := json.Marshal(c) - if err != nil { - panic(err) - } - return string(jsonMessageBytes) + "\n" -} - -// CopyMessage container for file copy messages -type CopyMessage struct { - Source string `json:"source"` - Target string `json:"target"` - Length int64 `json:"length"` -} - -// String string printer for copy message -func (c CopyMessage) String() string { - if !globalJSONFlag { - return fmt.Sprintf("‘%s’ -> ‘%s’\n", c.Source, c.Target) - } - copyMessageBytes, err := json.Marshal(c) - if err != nil { - panic(err) - } - return string(copyMessageBytes) + "\n" -} - -// MirrorMessage container for file mirror messages -type MirrorMessage struct { - Source string `json:"source"` - Targets []string `json:"targets"` - Length int64 `json:"length"` -} - -// String string printer for mirror message -func (s MirrorMessage) String() string { - if !globalJSONFlag { - return fmt.Sprintf("‘%s’ -> ‘%s’\n", s.Source, s.Targets) - } - mirrorMessageBytes, err := json.Marshal(s) - if err != nil { - panic(err) - } - return string(mirrorMessageBytes) + "\n" -} - -// ShareMessage container for share messages -type ShareMessage struct { - Expires time.Duration `json:"expire-seconds"` - PresignedURL string `json:"presigned-url"` -} - -// String string printer for share message -func (s ShareMessage) String() string { - if !globalJSONFlag { - return fmt.Sprintf("Succesfully generated shared URL with expiry %s, please share: %s\n", s.Expires, s.PresignedURL) - } - shareMessageBytes, err := json.Marshal(s) - if err != nil { - panic(err) - } - // json encoding escapes ampersand into its unicode character which is not usable directly for share - // and fails with cloud storage. convert them back so that they are usable - shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u0026"), []byte("&"), -1) - return fmt.Sprintf("%s\n", string(shareMessageBytes)) -} diff --git a/session-json.go b/session-json.go new file mode 100644 index 00000000..e85855f3 --- /dev/null +++ b/session-json.go @@ -0,0 +1,52 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "encoding/json" + "strings" + + "github.com/minio/mc/pkg/console" +) + +// SessionJSONMessage json container for session messages +type SessionJSONMessage struct { + SessionID string `json:"sessionid"` + Time string `json:"time"` + CommandType string `json:"command-type"` + CommandArgs []string `json:"command-args"` +} + +func (s sessionV2) String() string { + if !globalJSONFlag { + message := console.SessionID("%s -> ", s.SessionID) + message = message + console.Time("[%s]", s.Header.When.Local().Format(printDate)) + message = message + console.Command(" %s %s", s.Header.CommandType, strings.Join(s.Header.CommandArgs, " ")) + return message + "\n" + } + sessionMesage := SessionJSONMessage{ + SessionID: s.SessionID, + Time: s.Header.When.Local().Format(printDate), + CommandType: s.Header.CommandType, + CommandArgs: s.Header.CommandArgs, + } + sessionJSONBytes, err := json.Marshal(sessionMesage) + if err != nil { + panic(err) + } + return string(sessionJSONBytes) + "\n" +} diff --git a/share-json.go b/share-json.go new file mode 100644 index 00000000..5278440b --- /dev/null +++ b/share-json.go @@ -0,0 +1,45 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "time" +) + +// ShareMessage container for share messages +type ShareMessage struct { + Expires time.Duration `json:"expire-seconds"` + PresignedURL string `json:"presigned-url"` +} + +// String string printer for share message +func (s ShareMessage) String() string { + if !globalJSONFlag { + return fmt.Sprintf("Succesfully generated shared URL with expiry %s, please share: %s\n", s.Expires, s.PresignedURL) + } + shareMessageBytes, err := json.Marshal(s) + if err != nil { + panic(err) + } + // json encoding escapes ampersand into its unicode character which is not usable directly for share + // and fails with cloud storage. convert them back so that they are usable + shareMessageBytes = bytes.Replace(shareMessageBytes, []byte("\\u0026"), []byte("&"), -1) + return fmt.Sprintf("%s\n", string(shareMessageBytes)) +}