Files
mc/session-main.go
T
Harshavardhana 3caee383f0 Migrate to govendor to avoid limitations of godep
- over the course of a project history every maintainer needs to update
  its dependency packages, the problem essentially with godep is manipulating
  GOPATH - this manipulation leads to static objects created at different locations
  which end up conflicting with the overall functionality of golang.

  This also leads to broken builds on many platforms. There is no easier way out of
  this other than asking users to do 'godep restore' all the time which perhaps as
  a practice doesn't sound clean, also has its own set of problems.

- govendor on the other hand is a right tool but a stop gap tool until we wait for
  golangs official 1.5 version which fixes this vendoring issue once and for all.

- govendor makes sure that the import paths are re-written instead of manipulating
  GOPATH.

  This has advantages
    - no more compiled objects being referenced in GOPATH and build time GOPATH
      manging which leads to conflicts.
    - proper import paths referencing the exact package a project is dependent on.

  govendor is simple and provides the minimal necessary tooling to achieve this.

  For now this is the right solution.
2015-08-12 19:04:16 -07:00

186 lines
4.6 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)
session.Delete()
}
return
}
if !isSession(sid) {
console.Fatalf("Session ‘%s’ not found.\n", sid)
}
session, err := loadSessionV2(sid)
fatalIf(err)
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())
}
switch strings.TrimSpace(ctx.Args().First()) {
// list resumable sessions
case "list":
err := listSessions()
fatalIf(err)
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)
// 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
}
}