return exit code 1 on error

Fixes #132
This commit is contained in:
Nicola Murino 2020-06-20 14:30:46 +02:00
parent 8cb47817f6
commit b80abe6c05
12 changed files with 41 additions and 4 deletions

View file

@ -1,6 +1,8 @@
package cmd
import (
"os"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -44,6 +46,7 @@ Please take a look at the usage below to customize the options.`,
logger.DebugToConsole("Data provider successfully initialized")
} else {
logger.WarnToConsole("Unable to initialize data provider: %v", err)
os.Exit(1)
}
},
}

View file

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"os"
"strconv"
"github.com/spf13/cobra"
@ -42,6 +43,7 @@ Please take a look at the usage below to customize the startup options`,
err := winService.Install(serviceArgs...)
if err != nil {
fmt.Printf("Error installing service: %v\r\n", err)
os.Exit(1)
} else {
fmt.Printf("Service installed!\r\n")
}

View file

@ -73,12 +73,12 @@ Please take a look at the usage below to customize the serving parameters`,
fi, err := os.Stat(portableGCSCredentialsFile)
if err != nil {
fmt.Printf("Invalid GCS credentials file: %v\n", err)
return
os.Exit(1)
}
if fi.Size() > 1048576 {
fmt.Printf("Invalid GCS credentials file: %#v is too big %v/1048576 bytes\n", portableGCSCredentialsFile,
fi.Size())
return
os.Exit(1)
}
creds, err := ioutil.ReadFile(portableGCSCredentialsFile)
if err != nil {
@ -135,7 +135,9 @@ Please take a look at the usage below to customize the serving parameters`,
if err := service.StartPortableMode(portableSFTPDPort, portableSSHCommands, portableAdvertiseService,
portableAdvertiseCredentials); err == nil {
service.Wait()
os.Exit(0)
}
os.Exit(1)
},
}
)

View file

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
@ -21,6 +22,7 @@ var (
err := s.Reload()
if err != nil {
fmt.Printf("Error reloading service: %v\r\n", err)
os.Exit(1)
} else {
fmt.Printf("Service reloaded!\r\n")
}

View file

@ -1,6 +1,8 @@
package cmd
import (
"os"
"github.com/spf13/cobra"
"github.com/drakkan/sftpgo/service"
@ -31,7 +33,9 @@ Please take a look at the usage below to customize the startup options`,
}
if err := service.Start(); err == nil {
service.Wait()
os.Exit(0)
}
os.Exit(1)
},
}
)

View file

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
@ -37,6 +38,7 @@ var (
err := winService.RunService()
if err != nil {
fmt.Printf("Error starting service: %v\r\n", err)
os.Exit(1)
} else {
fmt.Printf("Service started!\r\n")
}

View file

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
@ -21,6 +22,7 @@ var (
status, err := s.Status()
if err != nil {
fmt.Printf("Error querying service status: %v\r\n", err)
os.Exit(1)
} else {
fmt.Printf("Service status: %#v\r\n", status.String())
}

View file

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
@ -21,6 +22,7 @@ var (
err := s.Stop()
if err != nil {
fmt.Printf("Error stopping service: %v\r\n", err)
os.Exit(1)
} else {
fmt.Printf("Service stopped!\r\n")
}

View file

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
@ -21,6 +22,7 @@ var (
err := s.Uninstall()
if err != nil {
fmt.Printf("Error removing service: %v\r\n", err)
os.Exit(1)
} else {
fmt.Printf("Service uninstalled\r\n")
}

View file

@ -290,6 +290,10 @@ func (p BoltProvider) addUser(user User) error {
return err
}
user.ID = int64(id)
user.LastQuotaUpdate = 0
user.UsedQuotaSize = 0
user.UsedQuotaFiles = 0
user.LastLogin = 0
for _, folder := range user.VirtualFolders {
err = addUserToFolderMapping(folder, user, folderBucket)
if err != nil {

View file

@ -192,6 +192,10 @@ func (p MemoryProvider) addUser(user User) error {
return fmt.Errorf("username %#v already exists", user.Username)
}
user.ID = p.getNextID()
user.LastQuotaUpdate = 0
user.UsedQuotaSize = 0
user.UsedQuotaFiles = 0
user.LastLogin = 0
user.VirtualFolders = p.joinVirtualFoldersFields(user)
p.dbHandle.users[user.Username] = user
p.dbHandle.usersIdx[user.ID] = user.Username

View file

@ -738,7 +738,9 @@ func TestUserFolderMapping(t *testing.T) {
u1 := getTestUser()
u1.VirtualFolders = append(u1.VirtualFolders, vfs.VirtualFolder{
BaseVirtualFolder: vfs.BaseVirtualFolder{
MappedPath: mappedPath1,
MappedPath: mappedPath1,
UsedQuotaFiles: 2,
UsedQuotaSize: 123,
},
VirtualPath: "/vdir",
QuotaSize: -1,
@ -753,6 +755,8 @@ func TestUserFolderMapping(t *testing.T) {
folder := folders[0]
assert.Len(t, folder.Users, 1)
assert.Contains(t, folder.Users, user1.Username)
assert.Equal(t, 0, folder.UsedQuotaFiles)
assert.Equal(t, int64(0), folder.UsedQuotaSize)
}
u2 := getTestUser()
u2.Username = defaultUsername + "2"
@ -793,7 +797,9 @@ func TestUserFolderMapping(t *testing.T) {
user2.VirtualFolders = nil
user2.VirtualFolders = append(user2.VirtualFolders, vfs.VirtualFolder{
BaseVirtualFolder: vfs.BaseVirtualFolder{
MappedPath: mappedPath2,
MappedPath: mappedPath2,
UsedQuotaFiles: 2,
UsedQuotaSize: 123,
},
VirtualPath: "/vdir",
QuotaSize: 0,
@ -807,6 +813,8 @@ func TestUserFolderMapping(t *testing.T) {
folder := folders[0]
assert.Len(t, folder.Users, 1)
assert.Contains(t, folder.Users, user2.Username)
assert.Equal(t, 0, folder.UsedQuotaFiles)
assert.Equal(t, int64(0), folder.UsedQuotaSize)
}
folders, _, err = httpd.GetFolders(0, 0, mappedPath1, http.StatusOK)
assert.NoError(t, err)