1
0
mirror of https://github.com/minio/mc.git synced 2025-11-12 01:02:26 +03:00
Files
mc/session-main.go
2015-08-19 17:11:34 -07:00

188 lines
4.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* 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 (
"os"
"sort"
"strings"
"github.com/minio/mc/internal/github.com/minio/cli"
"github.com/minio/mc/internal/github.com/minio/minio/pkg/probe"
"github.com/minio/mc/pkg/console"
)
// Help message.
var sessionCmd = cli.Command{
Name: "session",
Usage: "Manage sessions for cp and mirror",
Action: mainSession,
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
USAGE:
mc {{.Name}}{{if .Flags}} [ARGS...]{{end}} [SESSION] {{if .Description}}
DESCRIPTION:
{{.Description}}{{end}}{{if .Flags}}
FLAGS:
{{range .Flags}}{{.}}
{{end}}{{ end }}
EXAMPLES:
1. List sessions
$ mc {{.Name}} list
2. Resume session
$ mc {{.Name}} resume [SESSION]
3. Clear session
$ mc {{.Name}} clear [SESSION]|[all]
`,
}
// bySessionWhen is a type for sorting session metadata by time
type bySessionWhen []*sessionV2
func (b bySessionWhen) Len() int { return len(b) }
func (b bySessionWhen) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b bySessionWhen) Less(i, j int) bool { return b[i].Header.When.Before(b[j].Header.When) }
func listSessions() *probe.Error {
var bySessions []*sessionV2
for _, sid := range getSessionIDs() {
s, err := loadSessionV2(sid)
if err != nil {
return err.Trace()
}
bySessions = append(bySessions, s)
}
// sort sessions based on time
sort.Sort(bySessionWhen(bySessions))
for _, session := range bySessions {
console.Print(session)
}
return nil
}
func clearSession(sid string) {
if sid == "all" {
for _, sid := range getSessionIDs() {
session, err := loadSessionV2(sid)
fatalIf(err, "Unable to load session")
session.Delete()
}
return
}
if !isSession(sid) {
console.Fatalf("Session %s not found.\n", sid)
}
session, err := loadSessionV2(sid)
fatalIf(err, "Unable to load session")
if session != nil {
session.Delete()
}
}
func sessionExecute(s *sessionV2) {
switch s.Header.CommandType {
case "cp":
doCopyCmdSession(s)
case "mirror":
doMirrorCmdSession(s)
}
}
func mainSession(ctx *cli.Context) {
if len(ctx.Args()) < 1 || ctx.Args().First() == "help" {
cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code
}
if strings.TrimSpace(ctx.Args().First()) == "" {
cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code
}
if !isSessionDirExists() {
fatalIf(createSessionDir(), "Unable to create session")
}
switch strings.TrimSpace(ctx.Args().First()) {
// list resumable sessions
case "list":
fatalIf(listSessions(), "Unable to list sessions")
case "resume":
if len(ctx.Args().Tail()) != 1 {
cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code
}
if strings.TrimSpace(ctx.Args().Tail().First()) == "" {
cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code
}
sid := strings.TrimSpace(ctx.Args().Tail().First())
if _, err := os.Stat(getSessionFile(sid)); err != nil {
console.Fatalln(errInvalidSessionID{id: sid})
}
s, err := loadSessionV2(sid)
fatalIf(err, "Unable to load session")
// extra check for testing purposes
if s == nil {
return
}
{
savedCwd, err := os.Getwd()
if err != nil {
console.Fatalln("Unable to verify your current working folder. %s\n", err)
}
if s.Header.RootPath != "" {
// chdir to RootPath
os.Chdir(s.Header.RootPath)
}
sessionExecute(s)
{
err := s.Close()
if err != nil {
console.Fatalf("Unable to close session file properly. %s\n", err)
}
err = s.Delete()
if err != nil {
console.Fatalf("Unable to clear session files properly. %s\n", err)
}
}
// change dir back
os.Chdir(savedCwd)
}
// purge a requested pending session, if "*" purge everything
case "clear":
if len(ctx.Args().Tail()) != 1 {
cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code
}
if strings.TrimSpace(ctx.Args().Tail().First()) == "" {
cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code
}
clearSession(strings.TrimSpace(ctx.Args().Tail().First()))
default:
cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code
}
}