public static IEnumerable <string> GetErrors(this SshConnectionInfoValidationResult result)
        {
            if (result == SshConnectionInfoValidationResult.Valid)
            {
                yield break;
            }

            foreach (var value in Enum.GetValues(typeof(SshConnectionInfoValidationResult))
                     .Cast <SshConnectionInfoValidationResult>().Where(r => r != SshConnectionInfoValidationResult.Valid))
            {
                if ((value & result) == value)
                {
                    yield return(Resources.GetString($"{nameof(SshConnectionInfoValidationResult)}.{value}"));
                }
            }
        }
 public static string GetErrorString(this SshConnectionInfoValidationResult result, string separator = "; ") =>
 string.Join(separator, result.GetErrors());
コード例 #3
0
ファイル: App.xaml.cs プロジェクト: vincetm/FluentTerminal
        protected override async void OnActivated(IActivatedEventArgs args)
        {
            if (args is ProtocolActivatedEventArgs protocolActivated)
            {
                MainViewModel mainViewModel = null;

                if (!_alreadyLaunched)
                {
                    // App wasn't launched before double clicking a shortcut, so we have to create a window
                    // in order to be able to communicate with user.
                    mainViewModel = _container.Resolve <MainViewModel>();

                    await CreateMainView(typeof(MainPage), mainViewModel, true);
                }

                bool isSsh;

                try
                {
                    isSsh = _sshHelperService.Value.IsSsh(protocolActivated.Uri);
                }
                catch (Exception ex)
                {
                    await new MessageDialog($"Invalid link: {ex.Message}", "Invalid Link").ShowAsync();

                    mainViewModel?.ApplicationView.TryClose();

                    return;
                }

                if (isSsh)
                {
                    SshProfileViewModel connectionInfo;

                    try
                    {
                        connectionInfo = (SshProfileViewModel)_sshHelperService.Value.ParseSsh(protocolActivated.Uri);
                    }
                    catch (Exception ex)
                    {
                        await new MessageDialog($"Invalid link: {ex.Message}", "Invalid Link").ShowAsync();

                        mainViewModel?.ApplicationView.TryClose();

                        return;
                    }

                    SshConnectionInfoValidationResult result = connectionInfo.Validate();

                    if (result == SshConnectionInfoValidationResult.Valid)
                    {
                        await connectionInfo.AcceptChangesAsync();
                    }
                    else
                    {
                        // Link is valid, but incomplete (i.e. username missing), so we need to show dialog.
                        connectionInfo =
                            (SshProfileViewModel)await _dialogService.ShowSshConnectionInfoDialogAsync(connectionInfo);

                        if (connectionInfo == null)
                        {
                            // User clicked "Cancel" in the dialog.
                            mainViewModel?.ApplicationView.TryClose();

                            return;
                        }
                    }

                    if (mainViewModel == null)
                    {
                        await CreateTerminal(connectionInfo.Model, _applicationSettings.NewTerminalLocation);
                    }
                    else
                    {
                        await mainViewModel.AddTerminalAsync(connectionInfo.Model);
                    }

                    return;
                }

                await new MessageDialog($"Invalid link: {protocolActivated.Uri}", "Invalid Link").ShowAsync();

                mainViewModel?.ApplicationView.TryClose();

                return;
            }

            if (args is CommandLineActivatedEventArgs commandLineActivated)
            {
                if (string.IsNullOrWhiteSpace(commandLineActivated.Operation.Arguments))
                {
                    return;
                }

                _commandLineParser.ParseArguments(SplitArguments(commandLineActivated.Operation.Arguments), typeof(NewVerb), typeof(RunVerb), typeof(SettingsVerb)).WithParsed(async verb =>
                {
                    if (verb is SettingsVerb settingsVerb)
                    {
                        if (!settingsVerb.Import && !settingsVerb.Export)
                        {
                            await ShowSettings().ConfigureAwait(true);
                        }
                        else if (settingsVerb.Export)
                        {
                            var exportFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("config.json", CreationCollisionOption.OpenIfExists);

                            var settings = _settingsService.ExportSettings();
                            await FileIO.WriteTextAsync(exportFile, settings);
                        }
                        else if (settingsVerb.Import)
                        {
                            var file    = await ApplicationData.Current.LocalFolder.GetFileAsync("config.json");
                            var content = await FileIO.ReadTextAsync(file);
                            _settingsService.ImportSettings(content);
                        }
                    }
                    else if (verb is NewVerb newVerb)
                    {
                        var profile = default(ShellProfile);
                        if (!string.IsNullOrWhiteSpace(newVerb.Profile))
                        {
                            profile = _settingsService.GetShellProfiles().FirstOrDefault(x => x.Name.Equals(newVerb.Profile, StringComparison.CurrentCultureIgnoreCase));
                        }

                        if (profile == null)
                        {
                            profile = _settingsService.GetDefaultShellProfile();
                        }

                        if (!string.IsNullOrWhiteSpace(newVerb.Directory))
                        {
                            profile.WorkingDirectory = newVerb.Directory;
                        }

                        var location = newVerb.Target == Target.Default ? _applicationSettings.NewTerminalLocation
                            : newVerb.Target == Target.Tab ? NewTerminalLocation.Tab
                            : NewTerminalLocation.Window;

                        await CreateTerminal(profile, location).ConfigureAwait(true);
                    }
                    else if (verb is RunVerb runVerb)
                    {
                        var profile = new ShellProfile
                        {
                            Id               = Guid.Empty,
                            Location         = null,
                            Arguments        = runVerb.Command,
                            WorkingDirectory = runVerb.Directory
                        };

                        if (!string.IsNullOrWhiteSpace(runVerb.Theme))
                        {
                            var theme = _settingsService.GetThemes().FirstOrDefault(x => x.Name.Equals(runVerb.Theme, StringComparison.CurrentCultureIgnoreCase));
                            if (theme != null)
                            {
                                profile.TerminalThemeId = theme.Id;
                            }
                        }

                        if (string.IsNullOrWhiteSpace(profile.WorkingDirectory))
                        {
                            profile.WorkingDirectory = commandLineActivated.Operation.CurrentDirectoryPath;
                        }

                        var location = runVerb.Target == Target.Default ? _applicationSettings.NewTerminalLocation
                            : runVerb.Target == Target.Tab ? NewTerminalLocation.Tab
                            : NewTerminalLocation.Window;

                        await CreateTerminal(profile, location).ConfigureAwait(true);
                    }
                });
            }
        }
コード例 #4
0
        public string ConvertToUri(ISshConnectionInfo sshConnectionInfo)
        {
            SshConnectionInfoValidationResult result = sshConnectionInfo.Validate(true);

            if (result != SshConnectionInfoValidationResult.Valid)
            {
                throw new ArgumentException(result.GetErrorString(), nameof(sshConnectionInfo));
            }

            SshProfileViewModel sshConnectionInfoVm = (SshProfileViewModel)sshConnectionInfo;

            StringBuilder sb = new StringBuilder(sshConnectionInfoVm.UseMosh ? MoshUriScheme : SshUriScheme);

            sb.Append("://");

            bool containsUserInfo = false;

            if (!string.IsNullOrEmpty(sshConnectionInfoVm.Username))
            {
                sb.Append(HttpUtility.UrlEncode(sshConnectionInfoVm.Username));

                containsUserInfo = true;
            }

            if (!string.IsNullOrEmpty(sshConnectionInfoVm.IdentityFile))
            {
                sb.Append(";");

                if (!string.IsNullOrEmpty(sshConnectionInfoVm.IdentityFile))
                {
                    sb.Append($"{IdentityFileOptionName}={HttpUtility.UrlEncode(sshConnectionInfoVm.IdentityFile)}");
                }

                containsUserInfo = true;
            }

            if (containsUserInfo)
            {
                sb.Append("@");
            }

            sb.Append(sshConnectionInfoVm.Host);

            if (sshConnectionInfoVm.SshPort != SshProfile.DefaultSshPort)
            {
                sb.Append(":");
                sb.Append(sshConnectionInfoVm.SshPort.ToString("#####"));
            }

            bool queryStringAdded = false;

            if (sshConnectionInfoVm.UseMosh)
            {
                sb.Append(
                    $"?{ValidMoshPortsNames[0]}={sshConnectionInfo.MoshPortFrom:#####}-{sshConnectionInfo.MoshPortTo:#####}");

                queryStringAdded = true;
            }

            if (!sshConnectionInfo.TerminalThemeId.Equals(Guid.Empty))
            {
                if (!queryStringAdded)
                {
                    sb.Append("?");

                    queryStringAdded = true;
                }
                else
                {
                    sb.Append("&");
                }

                sb.Append($"{ThemeQueryStringParam}={sshConnectionInfo.TerminalThemeId}");
            }

            if (sshConnectionInfo.TabThemeId != 0)
            {
                if (!queryStringAdded)
                {
                    sb.Append("?");
                }
                else
                {
                    sb.Append("&");
                }

                sb.Append($"{TabQueryStringParam}={sshConnectionInfo.TabThemeId:##########}");
            }

            return(sb.ToString());
        }